What are the tips for DB2 database coding?

Source: Internet
Author: User

The following articles mainly describe some of the minor operation skills of the DB2 database program, I found a piece of information on some tips on DB2 coding on a reputable website for you to share. The following articles describe related content.
 

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.

2. Use a temporary table

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 database 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.

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;  

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. This method can only be used if the cursor is not closed due to a commit or rollback in a loop.

The other is

 
 
  1. pcursor1: for loopcs1 as cousor1 cursor as  
  2. select market_code as market_code  
  3. from tb_market_code  
  4. for update  
  5. do  
  6. end for;  

The advantage of this method is that it is relatively simple and does not need or allow) use open, fetch, close.

However, the with hold option cannot be used. If you want to use commit in a cursor loop, rollback cannot use this method. If you do not have the commit or rollback requirements, we recommend this method (it seems that there is a problem with the For method ).

How to modify the current record of a cursor

 
 
  1. update tb_market_code set market_code='0' where current of cursor1; 

However, be sure to define cursor1 as a modifiable cursor.

 
 
  1. declare cursor1 cursor for select market_code from tb_market_code  
  2. for update;  

For update cannot be used together with group by, DISTINCT, order by, for read only, UNION, EXCEPT for union all.

5. transcoding operations similar to decode

Oracle has a function select decode (a1, '1', 'n1 ', '2', 'n2', 'n3 ') aa1 from

This function is not available in the DB2 database, but it can be modified.

 
 
  1. select case a1  
  2. when '1' then 'n1'  
  3. when '2' then 'n2'  
  4. else 'n3'  
  5. end as aa1 from  

6. Search for the position of a character in a string similar to charindex

Locate ('y', 'dfdasfay ')

Find the location of 'y' in 'dfdasfay.

7. Calculate the number of different days for two dates similar to datedif

 
 
  1. days(date(‘2001-06-05’)) – days(date(‘2001-04-01’)) 

Days returns the number of days calculated from 0001-01-01.

8. UDF writing example

For C, see sqllib \ samples \ cli \ udfsrv. c.

9. Create a table containing the identity value (automatically generated ID)

Writing such a table

 
 
  1. CREATE TABLE test  
  2. (t1 SMALLINT NOT NULL  
  3. GENERATED ALWAYS AS IDENTITY  
  4. (START WITH 500, INCREMENT BY 1),  
  5. t2 CHAR(1));  

Only one column of identity is allowed in a table. The above content is an introduction to some minor operation skills of the DB2 database program. I hope you will gain some benefits.
 

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.