Dynamic SQL Format 4 SQL statement
Description
Use this format to execute a SQL statement
that produces a result set in which the number of input parameters,
or the number of result-set columns, or both, are unknown at compile
time.
Syntax
|
1 |
DECLARE Cursor | Procedure <br> DYNAMIC CURSOR | PROCEDURE <br> FOR DynamicStagingArea ; |
|
1 |
PREPARE DynamicStagingArea FROM SQLStatement<br> {USING TransactionObject} ; |
|
1 |
DESCRIBE DynamicStagingArea<br> INTO DynamicDescriptionArea ; |
|
1 |
OPEN DYNAMIC Cursor<br> USING DESCRIPTOR DynamicDescriptionArea ; |
|
1 |
EXECUTE DYNAMIC Procedure<br> USING DESCRIPTOR DynamicDescriptionArea ; |
|
1 |
FETCH Cursor | Procedure <br> USING DESCRIPTOR DynamicDescriptionArea ; |
|
1 |
CLOSE Cursor | Procedure ; |
|
Parameter |
Description |
|---|---|
|
Cursor or Procedure |
The name of the cursor or procedure you |
|
DynamicStagingArea |
The name of the DynamicStagingArea (usually SQLSA). If you need a DynamicStagingArea variable other than SQLSA, you must declare it and |
|
SQLStatement |
A string containing a valid SQL SELECT statement. The Enter a question mark (?) for each parameter in the statement. |
|
TransactionObject |
The name of the transaction object that |
|
DynamicDescriptionArea |
The name of the DynamicDescriptionArea If you need a DynamicDescriptionArea variable other than SQLDA, you must declare it and |
Usage
The DECLARE statement is not executable
and can be defined globally.
If your DBMS supports formats of FETCH other
than the customary (and default) FETCH NEXT,
you can specify FETCH FIRST, FETCH PRIOR,
or FETCH LAST.
To declare a local cursor or procedure, open the script in
the Script view and select Paste SQL from
the PainterBar or the Edit>Paste Special menu. To declare
a global, instance, or shared cursor or procedure, select Declare
from the first drop-down list in the Script view and Global Variables,
Instance Variables, or Shared Variables from the second drop-down
list, then select Paste SQL.
For information about global, instance, shared,
and local scope, see “Where to declare variables “.
Accessing attribute information
When a statement is described into a DynamicDescriptionArea,
this information is available to you in the attributes of that DynamicDescriptionArea
variable:
|
Information |
Attribute |
|---|---|
|
Number of input parameters |
NumInputs |
|
Array of input parameter types |
InParmType |
|
Number of output parameters |
NumOutputs |
|
Array of output parameter types |
OutParmType |
Setting and accessing parameter values
The array of input parameter values and the array of output
parameter values are also available. You can use the SetDynamicParm function
to set the values of an input parameter and the following functions
to obtain the value of an output parameter:
|
|
For information about these functions, see GetDynamicDate, GetDynamicDateTime, GetDynamicDecimal, GetDynamicNumber, GetDynamicString,
and GetDynamicTime.
Parameter values
The following enumerated datatypes are the valid values for
the input and output parameter types:
|
|
Input parameters
You can set the type and value of each input parameter found
in the PREPARE statement. PowerBuilder populates
the SQLDA attribute NumInputs
when the DESCRIBE is executed. You can use this
value with the SetDynamicParm function to set
the type and value of a specific input parameter. The input parameters
are optional; but if you use them, you should fill in all the values
before executing the OPEN or EXECUTE statement.
Output parameters
You can access the type and value of each output parameter
found in the PREPARE statement. If the database
supports output parameter description, PowerBuilder populates the SQLDA attribute NumOutputs when
the DESCRIBE is executed. If the database does
not support output parameter description, PowerBuilder populates
the SQLDA attribute NumOutputs
when the FETCH statement is executed.
You can use the number of output parameters in the NumOutputs
attribute in functions to obtain the type of a specific parameter
from the output parameter type array in the OutParmType attribute.
When you have the type, you can call the appropriate function after
the FETCH statement to retrieve the output value.
Examples
These statements assume 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 datatype by
wrapping the CHOOSE CASE statement in a loop
and expanding the CASE statements:
|
1 |
string Stringvar, Sqlstatement |
|
1 |
integer Intvar |
|
1 |
Long LongVar |
|
1 |
Sqlstatement = "SELECT emp_id FROM employee" |
|
1 |
PREPARE SQLSA FROM :Sqlstatement ; |
|
1 |
DESCRIBE SQLSA INTO SQLDA ; |
|
1 |
DECLARE my_cursor DYNAMIC CURSOR FOR SQLSA ; |
|
1 |
OPEN DYNAMIC my_cursor USING DESCRIPTOR SQLDA ; |
|
1 |
FETCH my_cursor USING DESCRIPTOR SQLDA ; |
|
1 |
// If the FETCH is successful, the output |
|
1 |
// descriptor array will contain returned |
|
1 |
// values from the first row of the result set. |
|
1 |
// SQLDA.NumOutputs contains the number of |
|
1 |
// output descriptors. |
|
1 |
// The SQLDA.OutParmType array will contain |
|
1 |
// NumOutput entries and each entry will contain |
|
1 |
// a value of the enumerated datatype ParmType |
|
1 |
// (such as TypeInteger!, TypeLongLong!, or |
|
1 |
// TypeString!). |
|
1 |
CHOOSE CASE SQLDA.OutParmType[1] |
|
1 |
CASE TypeString! |
|
1 |
Stringvar = GetDynamicString(SQLDA, 1) |
|
1 |
CASE TypeInteger! |
|
1 |
Intvar = GetDynamicNumber(SQLDA, 1) |
|
1 |
CASE TypeLongLong! |
|
1 |
Longvar = GetDynamicDecimal(SQLDA, 1) |
|
1 |
END CHOOSE |
|
1 |
CLOSE my_cursor ; |
These statements assume you know there is one string input descriptor
and sets the parameter to MA:
|
1 |
string Sqlstatement, sValue |
|
1 |
Sqlstatement = "SELECT emp_fname, emp_lname " & |
|
1 |
       + "FROM employee WHERE state = ?" |
|
1 |
PREPARE SQLSA FROM :Sqlstatement ; |
|
1 |
|
1 |
DESCRIBE SQLSA INTO SQLDA ; |
|
1 |
|
1 |
// If the DESCRIBE is successful, the input |
|
1 |
// descriptor array will contain one input |
|
1 |
// descriptor that you must fill prior to the OPEN |
|
1 |
|
1 |
DECLARE my_cursor DYNAMIC CURSOR FOR SQLSA ; |
|
1 |
SetDynamicParm(SQLDA, 1, "MA") |
|
1 |
|
1 |
OPEN DYNAMIC my_cursor USING DESCRIPTOR SQLDA ; |
|
1 |
|
1 |
FETCH my_cursor USING DESCRIPTOR SQLDA ; |
|
1 |
|
1 |
// If the FETCH is successful, the output |
|
1 |
// descriptor array will contain returned |
|
1 |
// values from the first row of the result set |
|
1 |
// as in the first example. |
|
1 |
|
1 |
// To test and see the values: |
|
1 |
sValue = SQLDA.GetDynamicString(1) |
|
1 |
//messagebox("",sValue) |
|
1 |
sValue = SQLDA.GetDynamicString(2) |
|
1 |
//messagebox("",sValue) |
|
1 |
Do While sqlca.sqlcode <> 100 |
|
1 |
   FETCH my_cursor USING DESCRIPTOR SQLDA ; |
|
1 |
       sValue = SQLDA.GetDynamicString(1) |
|
1 |
       //messagebox("",sValue) |
|
1 |
       sValue = SQLDA.GetDynamicString(2) |
|
1 |
      //messagebox("",sValue) |
|
1 |
Loop |
|
1 |
|
1 |
CLOSE my_cursor ; |
This example is for a stored procedure with a return value
for a SQL Native Client (SNC) connection:
|
1 |
integer var1, ReturnVal<br>string var2<br> <br>PREPARE SQLSA FROM "execute @rc = myproc @parm1=?, @parm2=? OUTPUT ";<br> <br>DESCRIBE SQLSA INTO SQLDA ;<br> <br>DECLARE my_proc DYNAMIC PROCEDURE FOR SQLSA ;<br> <br>SetDynamicParm(SQLDA, 1, var1)<br>SetDynamicParm(SQLDA, 2, var2)<br> <br>EXECUTE DYNAMIC my_proc USING DESCRIPTOR SQLDA ;<br> <br>//fetch result set<br>. . .<br> <br>//fetch return value and output parameter<br>FETCH my_proc USING DESCRIPTOR SQLDA ;<br> <br>//get return value<br>CHOOSE CASE SQLDA.OutParmType[1]<br>CASE TypeInteger!<br>   rc = GetDynamicNumber(SQLDA, 1)<br>CASE TypeLong!<br>   rc = GetDynamicNumber(SQLDA, 1)<br>CASE TypeString!<br>   Var2 = GetDynamicString(SQLDA, 1)<br>END CHOOSE<br> <br>//get output value<br> <br>CHOOSE CASE SQLDA.OutParmType[2]<br>CASE TypeString!<br>   Var2 = GetDynamicString(SQLDA, 2)<br>CASE TypeInteger!<br>   rc = GetDynamicNumber(SQLDA, 2)<br>CASE TypeLong!<br>   rc = GetDynamicNumber(SQLDA, 2)<br>END CHOOSE<br> <br>CLOSE my_proc ; |