Samples to execute dynamic SQL:
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 DYNSTMT PIC X(1024).
01 TABLENAME PIC X(100).
01 ROWS-TO-INSERT PIC S9(4) COMP.
01 TABLE-ROW.
03 S-SEQ-NUM-S PIC S9(8) COMP-3 OCCURS 4 TIMES.
03 S-NAME-S PIC X(8) OCCURS 4 TIMES.
03 S-ID-S PIC S9(4) COMP-3 OCCURS 4 TIMES.
03 SCORE-Y-S PIC X(4) OCCURS 4 TIMES.
01 I-TABLE-ROW.
03 I-S-SEQ-NUM-S PIC S9(4) COMP OCCURS 4 TIMES.
03 I-S-NAME-S PIC S9(4) COMP OCCURS 4 TIMES.
03 I-S-ID-S PIC S9(4) COMP OCCURS 4 TIMES.
03 I-SCORE-Y-S PIC S9(4) COMP OCCURS 4 TIMES.
EXEC SQL END DECLARE SECTION END-EXEC.
PROCEDURE DIVISION.
...
MOVE "V16TABLE" TO TABLENAME.
STRING "INSERT INTO ",
TABLENAME,
" VALUES(:V1, :V2, :V3, :V4)"
INTO DYNSTMT.
EXEC SQL PREPARE S FROM :DYNSTMT END-EXEC.
EXEC SQL EXECUTE S USING
:S-SEQ-NUM-S :I-S-SEQ-NUM-S,
:S-NAME-S :I-S-NAME-S,
:S-ID-S :I-S-ID-S,
:SCORE-Y-S :I-SCORE-Y-S
END-EXEC.