Avatar of huzefaq
huzefaq
 asked on

Having problem with db2 trigger

Hi guys

I have at trigger. What i need is that if I update the price column in the offerprice table it should place the old value of price in the compareprice column only in that row. But I am having the following problem with the trigger I have. i would really appreciate any help regarding this


CREATE TRIGGER mytrig    
after  UPDATE OF price          
ON offerprice
REFERENCING NEW AS N OLD as O
FOR EACH ROW
BEGIN ATOMIC
  update offerprice op set op.compareprice = O.price;
END

Then, when I updated the price column one row, it updated the offerprice column in the entire table.

e.g.
select * from offerprice

PRICE    COMPAREPRICE         OFFERID
   50                          -              1
   10                          -              2
   15                          -              3

update offerprice
set price = 20        
where offerid = 3      

select * from offerprice

PRICE    COMPAREPRICE         OFFERID
   50                       15               1
   10                       15               2
   20                       15               3
DB2Databases

Avatar of undefined
Last Comment
Member_2_2484401

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Member_2_2484401

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
huzefaq

ASKER
hey davelash

I tried thsi trigger

CREATE TRIGGER mytrig    
after  UPDATE OF price        
ON offerprice            
REFERENCING NEW AS N OLD as O  
FOR EACH ROW
BEGIN ATOMIC
  update offerprice op  
  set op.compareprice = O.price
  where op.offerid = o.offerid;
END

but I am getting this error

An unexpected token "END-OF-STATEMENT" was found following ".offerid = O.offerid".  Expected tokens may include:  "<delim_semicolon>".

Would you know why I am getting this error

Member_2_2484401


I don't know, my friend. It works fine for me. (DB2 for iSeries 5.3)

Try putting a semicolon at the end of the trigger.

-- DaveSlash
huzefaq

ASKER
Actually Daveslash i worked after i remove the begin Atomic  and END tags. But I don'tunderstand why it was the problem.

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Member_2_2484401


Weird, eh?