Advanced PowerBuilder

HomePrevious Lesson: Format 3
Next Lesson: Handling the Results

Format 4

This format is similar to Format 3, except that you don't know the result set, hence you can't issue a FETCH statement. Typically, this format is used to accept the SQL statement from the user (ad-hoc queries), and execute and present the results back to the user. The following example uses a simple SELECT statement:
String lSQLStr
Integer lParm1
// We are hard-coding the SQL here. But, you can accept
// from the user and use it for ad-hoc queries.
lSQLStr = "select product_description " + &
          " from product_master where product_balance > ?"
lParm1 = 10
PREPARE SQLSA from :lSQLStr ;
DESCRIBE SQLSA into SQLDA ;
DECLARE lCursor1 DYNAMIC CURSOR for SQLSA ;
// Setting value for the first variable in the
// WHERE clause.
SetDynamicParm( SQLDA, 1, lParm1 )
// Now, SQLDA has the values for the parameters.
OPEN DYNAMIC lCursor1 USING descriptor SQLDA ;
FETCH lCursor1 USING descriptor SQLDA ;
DO while SQLCA.SQLCODE = 0
   // Since we hard-coded the SQL, we know the result
   // set. You need to call different function depending
   // on the datatype. You can check datatype by referring
   // to OutParmType array of SQLDA.
   lb_1.AddItem( GetDynamicString( SQLDA, 1 ) )
   FETCH lCursor1 USING descriptor SQLDA ;
LOOP
close lCursor1 ;

As described earlier, the PREPARE command formats the input SQL statement using the information specified in the FROM clause, and populates SQLSA, the object specified after the PREPARE command, with these statements.
HomePrevious Lesson: Format 3
Next Lesson: Handling the Results