网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> PowerBuilder >> 文章正文
  在BP中使用ASE存储过程的例子            【字体:
在BP中使用ASE存储过程的例子
作者:佚名    文章来源:不详    点击数:    更新时间:2007-8-16    

This document describes a sample PowerBuilder application that demonstrates the use of a Adaptive Server Enterprise Stored Procedure. The sample procedure contains a result set of two columns, an input parameter, an output parameter, and a return value.

Following is a general overview of the commands covered in this example:

Create a Stored Procedure
Use the CREATE command to create the procedure in the database. You must be connected to the database, and a procedure with the same name can not already exist in the database. If the procedure already exists and you wish to re-create it, it must first be dropped from the database.

Declare a Stored Procedure
Use the DECLARE command to define the variables to be used for input and output parameters. Note that the procedure must already exist in the database before it can be declared successfully. The procedure must be declared in PowerBuilder before it is executed.

Execute a Stored Procedure
Use the EXECUTE command to execute the procedure on the database server.

Fetch the Stored Procedure results
Use the FETCH command to obtain the result set, output parameter value(s), and the return value. All rows from the result set are first fetched in a loop, then an additional fetch is used to obtain the output parameter(s) and the return value.

Drop the Stored Procedure
Use the DROP command, if desired, to delete the stored procedure form the database.
 

Sample PBL Description
 

In this example, a stored procedure is used to obtain a list of employees in a specific department. The procedure uses an input parameter containing the desired department id, and sets an output parameter to the count of employees in the selected department. In addition, a listbox is populated with the employee names from the procedure result set.

The following sections describes the objects and scripts used to CREATE, DECLARE, EXECUTE, FETCH, and DROP the stored procedure.

Error checking is included. If any of the scripts fail for any reason, a MessageBox is displayed containing error codes and the error message text.

Window instance variables

string is_msg, is_command
int input1
DECLARE myproc PROCEDURE FOR @rv = test_proc
       @inparm1 = :input1,
       @outparm1 = 0 OUTPUT;

The procedure DECLARE is done as a window instance variable in this example.

Command buttons
 

These are the command buttons defined on the window. See the next section for the scripts for each of these buttons.

cb_connect Connect to the database
cb_create Create the stored procedure in the database
cb_execute Execute the stored procedure
cb_fetch Fetch the result set, return code, and output parameter
cb_drop Drop the stored procedure from the database

Window Objects

lb_resultset List box that is populated with the data from the result set
 

Application Script

a_stored_proc

// Open the window
Open(w_syb10_sp)
 

Window command button Scripts

cb_connect
 

// Note: Please put appropriate values in these variables
SQLCA.DBMS="syc"
SQLCA.Database="dbname"
SQLCA.LogPass="password"
SQLCA.ServerName="server"
SQLCA.LogId="loginid"
SQLCA.AutoCommit = TRUE

// Connect to the database
CONNECT USING SQLCA;

// Display an error if the connect fails
IF SQLCA.SQLCode <> 0 THEN
  is_msg = "SQLCode: " + string(SQLCA.SQLCode)
  MessageBox("Connect Error", is_msg)
END IF
 

cb_create
 

// Populate the string variable with the SQL command to create the procedure.
// Note that in this example, the procedure will always return a 3 for the
// return value, since it is hardcoded in the procedure definition.

is_command = "create procedure test_proc @inparm1 int, @outparm1 int " + &
 "OUTPUT as " + &
 "BEGIN " + &
 "select @outparm1 = COUNT(emp_id) from employee where dept_id = @inparm1 " + &
 "select emp_fname, emp_lname from employee where dept_id = @inparm1 " + &
 "return 3 " + &
 "END"

// Use dynamic SQL to execute the Create command
EXECUTE IMMEDIATE :is_command;

// Display error information if the create failed
If SQLCA.SQLCode <> 0 THEN
is_msg = "SQLCode: " + String(SQLCA.SQLCode) + " DBCode: " + &
String(SQLCA.SQLDBCode) + " ReturnData: " + SQLCA.SQLReturnData + &
" DBErrText: " + String(SQLCA.SQLErrText)
MessageBox("CREATE failed", is_msg)
END IF
 

cb_execute
 

// Populate the input parameter with the desired value
input1 = 300            // Use Department ID 300 as selection criteria

// Execute the procedure
EXECUTE myproc;

  // Display error information if the execute failed
  If SQLCA.SQLCode <> 0 THEN
    is_msg = "SQLCode: " + String(SQLCA.SQLCode) + " DBCode: " + &
      String(SQLCA.SQLDBCode) + " ReturnData: " + SQLCA.SQLReturnData + &
      " DBErrText: " + String(SQLCA.SQLErrText)
      MessageBox("Execute failed", is_msg)
  END IF
 

cb_fetch
 

  // Define variables to hold the return value and output parameter
  int li_ret_value, output1

  // Define variables to hold the result set data from the fetch
  string ls_lname, ls_fname

  // Loop to Fetch the result set, one row at a time
  DO WHILE SQLCA.SQLCode = 0
     FETCH myproc INTO :ls_lname, :ls_fname;
     IF SQLCA.SQLCode = 0 THEN
         // Add the employee name to the listbox
         lb_ResultSet.AddItem(ls_fname + ls_lname)
     ELSE
         IF SQLCA.SQLCode <> 100 THEN
             MessageBox("FETCH error", SQLCA.SQLErrText)
             Return
         END IF
     END IF
  LOOP

// Fetch the return value and output parameter into the variables
FETCH myproc INTO :li_ret_value, :output1;

// Check to ensure the return value and output parameter were obtained
IF SQLCA.SQLCode = 0 THEN
// Display the return value and output parameter in a message box
     is_msg = "Return value: " + string(li_ret_value) + &
         " Output parameter value: " + string(li_ret_value)
     MessageBox("Results", is_msg)
  ELSE
// Display error
     is_msg = "SQLCode: " + String(SQLCA.SQLCode) + " DBCode: " + &
         String(SQLCA.SQLDBCode) + " ReturnData: " + SQLCA.SQLReturnData + &
         " DBErrText: " + String(SQLCA.SQLErrText)
     MessageBox("Execute of FETCH failed", is_msg)
  END IF
 

cb_drop
 

// populate the string variable with the SQL command to drop the procedure
  is_command = "DROP PROCEDURE test_proc"

// Use dynamic SQL to execute the drop command
  EXECUTE IMMEDIATE :is_command;

// Display error information if the drop failed
  IF SQLCA.SQLCode <> 0 THEN
     is_msg = "SQLCode: " + String(SQLCA.SQLCode) + " DBCode: " + &
         String(SQLCA.SQLDBCode) + " ReturnData: " + SQLCA.SQLReturnData + &
         " DBErrText: " + String(SQLCA.SQLErrText)
     MessageBox("DROP failed", is_msg)
  END IF  

站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     在PB程序中实现ODBC数据…
     用PB5 设计 串口通讯程序
     如何用VC创建可在PB中调…
     PB连接Microsoft  SQL S…
     用PB编写多线程应用程序
  • JSP中JavaBean的生命周期

  • 快速、简便使用AJAX技术的三…

  • bpm产品

  • Ant入门-配置和使用     选…

  • ant使用简介

  • java现状----编程使用的语言…

  • Cookie又见Cookie-使用Html…

  • 一个关于Comparator的使用例…

  • 关于JSP的隐式对象的使用(供…

  • 使用ICE遇到的编译问题

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David