Previous Topic: Classes of UseNext Topic: Dynamic SQL for Fixed-List SELECT Statements


Dynamic SQL for Non-SELECT Statements

The simplest use of dynamic SQL is when only non-SELECT statements are to be dynamically executed and the SQLDA does not have to be explicitly used.

Note: Because you know when you code this type of program how many parameter markers are to be included in the statement, you can code the USING clause of the EXECUTE statement with a list of variable names.

The steps taken by a program where only non-SELECT statements are dynamically executed are as follows:

  1. Read a statement containing parameter markers (for example, DELETE FROM CUSTOMERS WHERE CUSNO=?) into USERSTR.
  2. Do a PREPARE of USERSTMT.
        EXEC SQL
          PREPARE USERSTMT FROM :USERSTR
        END-EXEC
    
  3. Read a value for CUSNO from some list.
        DO UNTIL (CUSNO = 0)
          EXEC SQL
            EXECUTE USERSTMT USING :CUSNO
          END-EXEC
        ENDDO
    
  4. Read the next value for CUSNO from the list, and so on.
  5. Deal with any SQL return codes that indicate errors.