I. Dynamic execution of SQL
PREPARE from ' Delete from Test ' ; EXECUTE S1;
Second, using cursors
DECLARE CURSOR for SELECT from FJDC. V_i_dg_dm_zy_wl_zbhz_attr T; OPEN V_cursor; FETCH into V_deletesql,v_insertsql; CLOSE V_cursor;
Third, while loop
while true -do expression ENDwhile;
Iv. DB2 query table data, generate dynamic SQL, and execute
1 CREATE PROCEDURE" TEST". " TEST "()2 BEGIN3 DECLAREV_deletesqlVARCHAR( -);4 DECLAREV_insertsqlVARCHAR( -);5 DECLAREI_countINT;6 7 --determine the number of cycles8 SELECT COUNT(Insertsql) intoI_count fromFJDC. V_i_dg_dm_zy_wl_zbhz_attr T;9 IFI_count> 0 ThenTen --Cursors One DECLAREV_cursorCURSOR for A SELECTDeletesql,insertsql fromFJDC. V_i_dg_dm_zy_wl_zbhz_attr T; - - OPENV_cursor; the whileI_count> 0 Do - SETI_count=I_count-1; - FETCHV_cursor intoV_deletesql,v_insertsql; - + --Execute Dynamic SQL - PREPARES1 fromV_deletesql; + EXECUTES1; A at PREPARES1 fromV_insertsql; - EXECUTES1; - - END while; - CLOSEV_cursor; - in END IF; - END
The DB2 stored procedure implements query table data, generates dynamic SQL, and executes