Correct DB2 Programming Application

Source: Internet
Author: User

The following articles mainly describe DB2 programming. We first do not use the TAB key to describe it after creating a stored procedure. If you program DB2, if you are curious, the following articles will unveil its mysteries.

1.1 do not use the TAB key after creating a stored procedure

 
 
  1. create procedure  

You can only use spaces, but not tab keys, otherwise the compilation will fail.

Remember, remember.

1.2 use temporary tables

Note that temporary tables can only be created on user tempory tables space. If the database only has system tempory table space, temporary tables cannot be created.

In addition, the temporary tables of DB2 are not the same as those of sybase and oracle. The temporary tables of DB2 are valid in a session. Therefore, if the program has multiple threads, it is better not to use temporary tables, which is difficult to control.

It is best to add the with replace option when creating a temporary table so that the drop temporary table is not displayed, if you do not add this option when creating a temporary table and the temporary table has been created in this session and has not been dropped, an error occurs.

1.3 retrieve the specified first few records from the data table

 
 
  1. select * from tb_market_code fetch first 1 rows only  

However, the following method is not allowed.

 
 
  1. select market_code into v_market_code   
  2. from tb_market_code fetch first 1 rows only;   

Select the field of the first record to a variable in the following way

 
 
  1. declare v_market_code char(1);   
  2. declare cursor1 cursor for select market_code from tb_market_code   
  3. fetch first 1 rows only for update;   
  4. open cursor1;   
  5. fetch cursor1 into v_market_code;   
  6. close cursor1;   

1.4 Use of cursors

Note: commit and rollback

When using a cursor, pay special attention that if the with hold option is not added, the cursor will be closed during Commit and Rollback. There are many things to note about Commit and Rollback. Be careful

Two Methods for defining a cursor

One is

 
 
  1. declare continue handler for not found   
  2. begin   
  3. set v_notfound = 1;   
  4. end;   
  5. declare cursor1 cursor with hold for select market_code from tb_market_code for update;   
  6. open cursor1;   
  7. set v_notfound=0;   
  8. fetch cursor1 into v_market_code;   
  9. while v_notfound=0 Do   
  10. --work   
  11. set v_notfound=0;   
  12. fetch cursor1 into v_market_code;   
  13. end while;   
  14. close cursor1;   

This method is complex but flexible. In particular, you can use the with hold option. If there is a commit or rollback in the loop and the cursor should not be closed, you can only use this method. The above content is an introduction to the DB2 programming skills section, I hope you will have some gains.

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.