The DB2 stored procedure implements query table data, generates dynamic SQL, and executes

Source: Internet
Author: User
Tags db2

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.