DB2 cursor (problem during update)

Source: Internet
Author: User

DB2 cursor (update problem) this afternoon, I suddenly received the problem that the implementer sent back from the current site. One of the problems I could not find out why, this problem was not found until I searched for information online and tested the program again. The problem comes from a stored procedure of the database. Www.2cto.com Java code -- variable declaration --.... -- determine whether the cursor ends declare v_curOver integer default 0; -- declare the cursor declare c_CCDZ cursor for select nvl (COL1, '') from TABLE1 where COL2 = in_COL2; -- declare Exception Handling, this exception is triggered when the cursor ends (this sentence should be placed in the final declaration) declare continue handler for not found set v_curOver = 1; set v_curOver = 0; open c_CCDZ; CCDZLoop: loop -- Note: only fetch can cause exceptions. fetch c_CCDZ into v_COL1; if v_curOver = 1 then leave CCDZLoop; end if; update TABLE2 set COL11 = '01' where COL = v_COL1; end loop CCDZLoop; close c_CCDZ; update another table in one cursor. Because I used to write this in the query operation before, but it does not involve the addition and modification operation. At that time, I tested the general idea and tried a data randomly (I didn't expect that there was only one record of the data ), no problem. As a result, the following problem occurs: There are N records in the cursor, but an update statement is executed, and the rest is ignored. Then I checked the information online to understand the above definition, during the update operation, the cursor will jump to the end, so only the first record is updated. So I used another method. Java code declare cursor1 cursor for select T1_COL2 from TABLE1 where T1_COL1 = in_COL1; select count (1) into v_NUM from TABLE1 where partition = in_COL1; open cursor1; partition: loop fetch cursor1 into v_COL2; if v_curOver = v_NUM then leave cursorLoop1; end if; -- update TABLE2 set COL2 = '01' where COL1 = v_COL2; set v_curOver = v_curOver + 1; end loop cursorLoop1; close cursor1; in this way, you can update the data as expected. Of course, the above is just my reference to the methods I have come up with on-line materials. Here we will give you a better way to learn more! To sum up, method 1 is suitable for queries and loops are made by judging the cursor status. method 1 is suitable for updates, but you need to calculate the number of cycles by yourself.

Related Article

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: info-contact@alibabacloud.com 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.