If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Cursor selecting, updating and inserting data

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-07-09, 11:31
frun frun is offline
Registered User
 
Join Date: Apr 2009
Posts: 21
Question Cursor selecting, updating and inserting data

Hi,

i need to create stored procedure with four arguments
IN T1 TIMESTAMP,
IN T2 TIMESTAMP,
IN COEFFICIENT INTEGER,
OUT ROWS_AFFECTED

This procedure should read rows from table TAB1 and mark rows with date between T1 and T2 as invalid (by setting STATE flag to 0, for example), but it also shoud create new rows with some old values and with the new value -COEFFCIENT and return numbr of processed rows.

Something like this:

Code:
CREATE PROCEDURE SP1 (args...)
BEGIN

? CREATE CURSOR c1 FOR SELECT ID, VAL1, VAL2, Tx FROM TAB1 WHERE (T1 < Tx AND Tx < T2);

OPEN c1;

?  ROWS_AFFECTED = c1.NUMEBER_OF_ROWS in the cursor

? WHILE READ FROM CURSOR, FETCH or something like this---

? UPDATE TAB1 SET STATE = 0 WHERE ID = c1.ID;

? INSERT INTO TAB1 (VAL1, VAL2, COEFF, Tx) VALUES (c1.VAL1, c1.VAL2, COEFF, c1.Tx);

CLOSE c1;

END;

But there is a lot of things I don't know how to write. Is it posible to create cursor for select that updates data? How get number of affected rows, what is the syntax and so on...

Can somebody help me, please?

DB2 v9.5.0.808 (Express), OS Win 2003 Server

Thanks a lot.
Reply With Quote
  #2 (permalink)  
Old 05-07-09, 11:41
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Unless you are doing complex algorithms on the data for the insert, you can do it without the cursor. Something like this:

Code:
CREATE PROCEDURE SP1 (args...)
BEGIN
 INSERT INTO TAB1 (VAL1, VAL2, COEFF, Tx) select c1,c2,c3,c4 from ...


 UPDATE TAB1 SET STATE = 0 WHERE ...

CLOSE c1;

END@

Andy
Reply With Quote
  #3 (permalink)  
Old 05-07-09, 11:54
frun frun is offline
Registered User
 
Join Date: Apr 2009
Posts: 21
Yes, but in that case row that was not in the SELECT can be UPDATED even if the WHERE is the same, cannot?
Reply With Quote
  #4 (permalink)  
Old 05-07-09, 12:36
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
There would be a slight possibility, depending on the concurrency rules being used. You could declare a temp table insert the rows into it, then do the update based on the contents of the temp table then do the insert also based on the contents of the temp table.

You could use a cursor, but then you have to declare a condition holder, then a loop to process through the cursor. I just try to avoid them if possible.

Andy
Reply With Quote
  #5 (permalink)  
Old 05-08-09, 03:22
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Besides: if you can use set-oriented processing in SQL instead of procedural logic, you have a very good chance of achieving better performance. The reason is that databases systems are/were tuned for decades to set processing.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On