EXECUTE IMMEDIATE和EXECUTE PREPARE是动态SQL的形式。在立即执行EXECUTE的情况下,我们可以在主机变量中给出SQL语句,然后在EXECUTE IMMEDIATE中传递此主机变量。
下面的示例演示了这些形式。
01 WS-SQL-DECLARE 05 WS-SQL-LEN PIC S9(04) COMP. 05 WS-SQL-QUERY PIC X(70). MOVE +80 TO WS-SQL-LEN MOVE “UPDATE ORDERS SET ORDER_PAID = ‘YES’ WHERE ORDER_DATE = ‘14-08-2020’” TO WS-SQL-QUERY EXEC SQL EXECUTE IMMEDIATE :WS-SQL-DECLARE END-EXEC
如果是EXECUTE PREPARE,则首先准备SQL语句,然后执行。我们可以使用这种形式的动态SQL,如下所示。
01 WS-SQL-DECLARE 05 WS-SQL-LEN PIC S9(04) COMP. 05 WS-SQL-QUERY PIC X(70). MOVE +80 TO WS-SQL-LEN MOVE “UPDATE ORDERS SET ORDER_PAID = ‘YES’ WHERE ORDER_DATE = ‘14-08-2020’” TO WS-SQL-QUERY EXEC SQL PREPARE RUNFORM FROM :WS-SQL-DECLARE END-EXEC EXEC SQL EXECUTE RUNFORM END-EXEC