EXECUTE IMMEDIATE和带有PREPARE的EXECUTE不能用于SELECT查询。对于SELECT查询,我们有一个固定列表SELECT,其中要提取的列保持不变,并且无法更改。
例如,如果我们要选择在14-08-2020下的订单。然后我们可以使用如下所示的动态SQL:
MOVE ‘SELECT ORDER_ID FROM ORDERS WHERE ORDER_DATE=?’ TO WS-SQL-QUERY EXEC SQL DECLARE ORDER-CUR CURSOR FOR SELQUERY END-EXEC EXEC SQL PREPARE SELQUERY FROM :WS-SQL-QUERY END-EXEC MOVE ‘14-08-2020’ TO WS-ORDER-DATE EXEC SQL OPEN ORDER-CUR USING :WS-ORDER-DATE END-EXEC PERFORM UNTIL SQLCODE = +100 EXEC SQL FETCH ORDER-CUR INTO :WS-ORDER-ID END-EXEC END-PERFORM EXEC SQL CLOSE ORDER-CUR END-EXEC