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 help needed

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-20-06, 13:13
bobjohnson360 bobjohnson360 is offline
Registered User
 
Join Date: Jan 2006
Posts: 15
cursor help needed

I need a trigger that updates a second table but the
trigger below seems to update all the rows not the one
only.

update on_hand set on_hand.qty = on_hand.qty - ( Select a.qty from adjust a, on_hand o where a.part = o.part);

( I was told a cursor would help with this problem)

table structure
table ADJUST
ID INTEGER
PART CHAR
QTY INTEGER

TABLE ON_HAND
ID INTEGER
PART CHAR
QTY INTEGER

GOAL : substact ( qty on_hand - qty adjust )

Adjust only has 1 row of data.
on_hand has many unique rows.

example data: from on_hand

1----a1000-----100
2----a2000-----100
3----a3000-----100

Any ideas/thanks
BJ
Reply With Quote
  #2 (permalink)  
Old 01-20-06, 13:48
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Have a look at this thread ..

Update Statment with complex Where clause

Does this help ?

BTW, you have mentioned 'trigger' . Can you post the trigger code

HTH

Sathyaram

Quote:
Originally Posted by bobjohnson360
I need a trigger that updates a second table but the
trigger below seems to update all the rows not the one
only.

update on_hand set on_hand.qty = on_hand.qty - ( Select a.qty from adjust a, on_hand o where a.part = o.part);

( I was told a cursor would help with this problem)

table structure
table ADJUST
ID INTEGER
PART CHAR
QTY INTEGER

TABLE ON_HAND
ID INTEGER
PART CHAR
QTY INTEGER

GOAL : substact ( qty on_hand - qty adjust )

Adjust only has 1 row of data.
on_hand has many unique rows.

example data: from on_hand

1----a1000-----100
2----a2000-----100
3----a3000-----100

Any ideas/thanks
BJ
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 01-20-06, 15:14
bobjohnson360 bobjohnson360 is offline
Registered User
 
Join Date: Jan 2006
Posts: 15
cursor help

Here is my trigger:

CREATE TRIGGER BOB.ADJUST4 AFTER INSERT ON BOB.ADJUST FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
update on_hand set on_hand.qty = on_hand.qty - ( Select a.qty from adjust a, on_hand o where a.part = o.part);
END

In the mean time I will look at the link you supplied
and see if that shades some light on my issue.
------------Thanks ----------
BJ
Reply With Quote
  #4 (permalink)  
Old 01-20-06, 17:23
craigmc craigmc is offline
Registered User
 
Join Date: Aug 2003
Location: austin,tx
Posts: 90
You need a where clause in your trigger to specify which row(s) in the on_hand table you want to update, ie the row that corresponds to the ADJUST item that the trigger is firing for.
It will probably be similar to the where clause you are using to select the qty in the subselect you have.


Something like:
CREATE TRIGGER BOB.ADJUST4 AFTER INSERT ON BOB.ADJUST FOR EACH ROW
REFERENCING NEW as INSERTED
MODE DB2SQL
BEGIN ATOMIC
update on_hand set on_hand.qty = on_hand.qty - ( Select a.qty from adjust a, on_hand o where a.part = o.part)
WHERE INSERTED.part = on_hand.part
END
Reply With Quote
  #5 (permalink)  
Old 01-20-06, 17:27
craigmc craigmc is offline
Registered User
 
Join Date: Aug 2003
Location: austin,tx
Posts: 90
Also, using the REFERENCING clause to give the update row a name, you can lose the subselect you are doing.

ex:
update on_hand set on_hand.qty = on_hand.qty - INSERTED.qty
WHERE INSERTED.part = on_hand.part
Reply With Quote
  #6 (permalink)  
Old 01-20-06, 19:32
bobjohnson360 bobjohnson360 is offline
Registered User
 
Join Date: Jan 2006
Posts: 15
trigger update code

craigmc


I ran the code you supplied in pane # 4
including the referencing statement and it seems to
function perfectly !!
I have been looking for this solution for some time.
-----------Thanks very much -------------
BJ
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