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 > Before update trigger. Can we update the New table?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-30-04, 07:51
AStefan AStefan is offline
Registered User
 
Join Date: Jun 2004
Posts: 57
Before update trigger. Can we update the New table?

Is it possible to update the NEW table in a before trigger?
I must update a column in a table if some conditions are satisfied.
In Oracle is posible to update this column using a before update trigger.
In sql server is not.
Can you tell me please if in Db2 it is possible such an update?
Thanks.
Reply With Quote
  #2 (permalink)  
Old 06-30-04, 07:58
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Yes (in DB2 UDB V8.1 LUW).

Andy
Reply With Quote
  #3 (permalink)  
Old 07-01-04, 08:27
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
Example:

CREATE TRIGGER triggername
NO CASCADE BEFORE INSERT ON table
REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
SET n.col = somevalue;
END
Reply With Quote
  #4 (permalink)  
Old 07-01-04, 09:20
AStefan AStefan is offline
Registered User
 
Join Date: Jun 2004
Posts: 57
it doesn't work!

I have the last DB2 version but Express. Could this be the reason?
Reply With Quote
  #5 (permalink)  
Old 07-01-04, 09:23
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
Not familiar with the version you describe. You may have to use an AFTER trigger (which is a drawback since you will be doing an extra transaction to the table).
Reply With Quote
  #6 (permalink)  
Old 07-01-04, 09:39
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Can you post your table structure and the statement you used ...

AFAIK, DB2 Express is functionally equivalent to other versions for most of the basic database stuff

Cheers
sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #7 (permalink)  
Old 07-02-04, 00:53
AStefan AStefan is offline
Registered User
 
Join Date: Jun 2004
Posts: 57
This is the table

CREATE TABLE NMSBin ( id decimal(31, 0) IDENTITY (1, 1) NOT NULL ,
timestamp decimal(31, 0) NOT NULL,
workflow_id decimal(31, 0),
name nvarchar (100) NOT NULL ,
description nvarchar (500),
CONSTRAINT PK_NMSBin PRIMARY KEY (id)
)

In Oracle the trigger looks like this one:

CREATE OR REPLACE TRIGGER bu_NMSBin
BEFORE UPDATE ON NMSBin
FOR EACH ROW
begin
if :new.id <> ld.id then
Raiserror
end if;
if :new.timestamp < ld.timestamp then
Raiserror
else
select s_NMSBin.nextval into :new.timestamp from dual;
end if;
end;


First of all I test the new id if it.s ok.
Secondly I test the old timestamp if it's smaller than the new timestamp.
If it isn't I increment the new timestamp using a sequence.
Here is the update on :new pseudotable and in db2 I try and try and I could't update the new table (select s_NMSBin.nextval into :new.timestamp from dual
In SQL Server there aren't any before update triggers and it is not posible to update the inserted table, so I use only stored procedures instead of triggers.
Probably I will use the same method in DB2 unless you can help me and show me the way to update the new table.
Thank you very much for your support, it was and it is very precious for me.
Reply With Quote
  #8 (permalink)  
Old 07-02-04, 09:22
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
CREATE TRIGGER triggername
NO CASCADE BEFORE UPDATE ON table
REFERENCING NEW AS N OLD AS O FOR EACH ROW MODE DB2SQL
WHEN ( n.timestamp = o.timestamp )
BEGIN ATOMIC
SET n.timestamp = NEXTVAL for s_NMSBin.nextval;
END
@
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