SQL Immediate Statement

The EXECUTE IMMEDIATE statement: Prepares an executable form of an SQL statement from a character string form of the statement, and then Executes the SQL statement. EXECUTE IMMEDIATE combines the basic functions of the PREPARE and EXECUTE statements. It can be used to prepare and execute SQL statements that contain neither host variables nor parameter markers.


     C*----------------------------------------------------------------
     C* MainLine
     C*----------------------------------------------------------------
      *
      * Alocate the file object
      *
     C                   CALL      'QCMDEXC'
     C                   PARM      Command(1)    CMD              80
     C                   PARM      80            LEN              15 5
     C                   Eval      CurrStep = CurrStep + 1
      *
      * First, Delete all records
      *
     C                   Eval      SQLImmedStm =
     C                             'Delete from INVADSPF'
     C/Exec SQL
     C+                  Execute Immediate :SQLImmedStm
     C/End-Exec
     C                   Eval      CurrStep = CurrStep + 1
      *
      * Second, insert the records
      *
     C                   Eval      SQLImmedStm =
     C                             'Insert Into INVADSPF (IDITEM,IDSTAT,' +
     C                             ' IDEDATE)'                            +
     C                             ' Select HITEM#,'                      +
     C                             '  Case When HISTAT < ''7'' then 05'   +
     C                             '       else 50'                       +
     C                             ' End as Status,'                      +
     C                             ' Decimal(''20'''                      +
     C                             '  concat substr(char(curdate()),7,2)' +
     C                             '  concat substr(char(curdate()),1,2)' +
     C                             '  concat substr(char(curdate()),4,2))'+
     C                             ' From INVIHDPF'                       +
     C                             ' Where HIASSB = ''Y'''                +
     C                             '  Or   HISUBA = ''Y'''
     C/Exec SQL
     C+                  Execute Immediate :SQLImmedStm
     C/End-Exec
     C                   Eval      CurrStep = CurrStep + 1
     C*----------------------------------------------------------------

When an EXECUTE IMMEDIATE statement is executed, the specified statement string is parsed and checked for errors. If the SQL statement is not valid, it is not executed and the error condition that prevents its execution is reported in the SQLCA. If the SQL statement is valid, but an error occurs during its execution, that error condition is reported in the SQLCA.