Basic Points of DB2 programming (1)

Source: Internet
Author: User

1 DB2 Programming

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

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

select  *  from tb_market_code fetch first 1 rows only

However, the following method is not allowed:

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

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

declare v_market_code char(1);
declare cursor1 cursor for select market_code from tb_market_code  
fetch first 1 rows only for update;
open cursor1;
fetch cursor1 into v_market_code;
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 especially careful.

Two methods to define a cursor:

One is:

declare continue handler for not found 
begin 
set v_notfound = 1; 
end; 
declare cursor1 cursor with hold for 

select market_code from tb_market_code  for update; 
open cursor1; 
set v_notfound=0; 
fetch cursor1 into v_market_code; 
while v_notfound=0 Do 
--work 
set v_notfound=0; 
fetch cursor1 into v_market_code; 
end while; 
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:

pcursor1: for loopcs1 as  cousor1  cursor  as 
select  market_code  as market_code
from tb_market_code
for update
do
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

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

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

declare cursor1 cursor for select market_code from tb_market_code  
for update;

For update cannot be used together with group by, DISTINCT, order by, for read only, UNION, EXCEPT for union all.
1.5 decode-like transcoding operations
Oracle has a function

select decode(a1,'1','n1','2','n2','n3') aa1 from

Db2 does not have this function, but it can be modified.

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

1.6 search for the position of a character in a string similar to charindex

Locate ('y', 'dfdasfay ')
Find the location of 'y' in 'dfdasfay.

1.7 calculate the number of different days for two dates similar to datedif

Days (date ('1970-06-05 ')-days (date ('1970-04-01 '))
Days returns the number of days calculated from 0001-01-01.

1.8 UDF writing example

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

1.9 create a table with the identity value (automatically generated ID)

Writing a table like this:

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

Only one column of identity is allowed in a table.

1.10 prevent handling of Null Field Values

SELECT DEPTNO ,DEPTNAME ,COALESCE(MGRNO ,'ABSENT'),ADMRDEPT
FROM DEPARTMENT

The COALESCE function returns the first non-empty expression in the () expression list. Multiple Expressions can be included.
It is similar to isnull in oracle, but isnull seems to have only two expressions.

1.11 retrieve the number of processed records

Declare v_count int;
Update tb_test set t1 = '0'
Where t2 = '2 ';
-- Check the number of modified rows to determine whether the specified record exists
Get diagnostics v _ count = ROW_COUNT;

Only applies to update, insert, and delete.
Invalid select


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.