The db2 tutorial is: DB2 programming skills (1 ).
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 careful
Two Methods for defining 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
Select market_code as market_code
From tb_market_code
For update
Do
End;
The advantage of this method is that it is relatively simple, and open, fetch, and close are not used (or allowed.
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 ('2017-06-05 ')-days