[转帖]如何在PowerBuilder中调用MS SQL SERVER的存储过程
<table cellspacing="0" cellpadding="0" width="681" border="0"><tbody><tr><!--Element not supported - Type: 8 Name: #comment--><!--Element not supported - Type: 8 Name: #comment--><td valign="top" width="651"><table height="1760" cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td height="1762"><center><table width="97%" border="0"><tbody><tr><td><p><!--Element not supported - Type: 8 Name: #comment--></p><p><font face="Helvetica, Arial, sans-serif"><b>Overview</b></font></p><p><font face="Helvetica, Arial, sans-serif">The examples in this document are intended to extend the information provided with our Online Documentation and manuals. </font></p><p><font face="Helvetica, Arial, sans-serif"></font></p><p><font face="Helvetica, Arial, sans-serif"><b>How do I get the output parameters back from the server if my stored procedure is also returning a resultset?</b>
</font></p><p><font face="Helvetica, Arial, sans-serif">There are a couple of different ways to get the RESULTSET and OUTPUT parameter from a stored procedure.</font></p><p><font face="Helvetica, Arial, sans-serif"><b>Code Sample 1: DECLARE, EXECUTE and FETCH ResultSet and Output parameters:<br/></b></font><font face="Helvetica, Arial, sans-serif"><br/>The first is to declare, execute and fetch the resultset and then when the sqlcode = 100 get out of loop and fetch the procedure a second time. The code would look like this: </font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">//declare local variable</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">LONG l_parm1</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">LONG l_out_parm</font>
<font face="Helvetica, Arial, sans-serif"></font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">STRING s_message</font>
<font face="Helvetica, Arial, sans-serif"></font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">CONNECT USING SQLCA;</font>
<font face="Helvetica, Arial, sans-serif"></font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">//Initialize the input parameter - this could be hard coded.</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">l_parm1 = 35</font>
<font face="Helvetica, Arial, sans-serif"></font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">DECLARE testproc PROCEDURE FOR dbo.testproc @Parm1 = :l_parm1, @OutParm = :l_out_parm OUTPUT USING SQLCA;</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">EXECUTE testproc;</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">//First, fetch the RESULTSET</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">do while sqlca.sqlcode = 0</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">FETCH testproc INTO :s_message;</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">if sqlca.sqlcode = 0 then</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">MessageBox( "s_message", s_message)</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">end if</font>
<font face="Helvetica, Arial, sans-serif"></font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">loop</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">//Now fetch the OUTPUT PARM</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">FETCH testproc INTO :l_out_parm;</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">MessageBox( "l_out_parm", String(l_out_parm))</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">CLOSE testproc;</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">DISCONNECT USING SQLCA;</font>
<font face="Helvetica, Arial, sans-serif"></font></p><p><font face="Helvetica, Arial, sans-serif">The second way this can be accomplished is by using the RPC method. This is discussed in the "Application Techniques" manual.<br/></font></p><p><font face="Helvetica, Arial, sans-serif"><b>Code sample 2:</b>
<b> Dynamic SQL Format 4 declaring a stored procedure</b><br/></font></p><p><font face="Helvetica, Arial, sans-serif">The sample in our help file illustrates ways to use <i>format 4</i> using a declared cursor. This script uses Format 4 embedded SQL statements and a declared stored procedure. This example assumes you know that there will be only one output descriptor and that it will be an integer. You can expand this example to support any number of output descriptors and any data type by wrapping the CHOOSE CASE statement in a loop and expanding the CASE statements. </font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">string ls_procname, ls_sql, ls_Temp</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">int li_job_id, li_Ctr, li_Temp</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">int li_rtn</font>
<font face="Helvetica, Arial, sans-serif"></font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">li_job_id = dw_emp.getitemNumber(1, "job_id")</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">setNull(li_job_id)</font>
<font face="Helvetica, Arial, sans-serif"></font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">ls_procname = 'pr_405237'</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">ls_sql = 'execute ' + ls_procname + ' @job_id=' + '?'</font>
<font face="Helvetica, Arial, sans-serif"></font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">PREPARE SQLSA FROM :ls_sql using sqlca;</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">DESCRIBE SQLSA INTO SQLDA ;</font>
<font face="Helvetica, Arial, sans-serif"></font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">DECLARE my_procudure DYNAMIC PROCEDURE FOR SQLSA ;</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">li_rtn = SQLDA.SetDynamicParm(1, li_job_id)</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">sle_1.Text = String(li_rtn)</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">EXECUTE DYNAMIC my_procudure USING DESCRIPTOR SQLDA ;</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">FETCH my_procudure USING DESCRIPTOR SQLDA ;</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1">If Sqlca.Sqlcode <> 0 then</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1"> Messagebox("Error ", String(Sqlca.Sqlcode) + sqlca.sqlerrtext)</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1"> else</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1"> for li_Ctr = 1 to sqlda.NumOutputs</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1"> CHOOSE CASE SQLDA.OutParmType</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1"> CASE TypeString!</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1"> ls_Temp = GetDynamicString(SQLDA, li_Ctr)</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1"> CASE TypeInteger!</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1"> li_Temp = GetDynamicNumber(SQLDA, li_Ctr)</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1"> END CHOOSE</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1"> next</font>
<font face="Helvetica, Arial, sans-serif"></font><font face="Helvetica, Arial, sans-serif"><br/></font><font face="Helvetica, Arial, sans-serif" size="-1"> end if</font>
<font face="Helvetica, Arial, sans-serif"></font></p><p><font face="Helvetica, Arial, sans-serif" size="-1">CLOSE my_procedure ;</font>
<font face="Helvetica, Arial, sans-serif"></font></p><!--Element not supported - Type: 8 Name: #comment--></td></tr></tbody></table></center></td></tr></tbody></table></td></tr><tr><!--Element not supported - Type: 8 Name: #comment--><td width="651"></td></tr></tbody></table>
页:
[1]