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 > Simple trigger help needed, Update who & when

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-19-03, 12:54
janrune janrune is offline
Registered User
 
Join Date: Aug 2003
Location: Austin, Texas
Posts: 12
Simple trigger help needed, Update who & when

Hi...
I have a simple question for one who is experienced...
In all our tables we have the two fields Edit_Who and Edit_When:
KL_Edit_Who VarChar(30) DEFAULT USER,
KL_Edit_When TIMESTAMP DEFAULT CURRENT TIMESTAMP,

In order to get those updated on each update we have to revert to triggers.
So I wrote two versions neither works...
CREATE TRIGGER KL_Key_LastGen_Upd
AFTER INSERT ON DBO.KL_Key_LastGen
FOR EACH ROW MODE DB2SQL
UPDATE DBO.KL_Key_LastGen SET KL_Edit_Who = USER, KL_Edit_When = CURRENT TIMESTAMP

CREATE TRIGGER KL_Key_LastGen_Upd
AFTER INSERT ON DBO.KL_Key_LastGen
REFERENCING NEW_TABLE AS NEWEMPS
FOR EACH STATEMENT MODE DB2SQL
UPDATE DBO.KL_Key_LastGen K SET KL_Edit_Who = USER, KL_Edit_When = CURRENT TIMESTAMP
WHERE KL_Table_Key IN( SELECT NEWEMPS.KL_Table_Key FROM NEWEMPS)

Anyone care to shed some light on my errors ???
Thanks
JR
__________________
JR Andreassen
janrune_NOSPAM@io.com
Reply With Quote
  #2 (permalink)  
Old 08-19-03, 14:25
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 3,935
Re: Simple trigger help needed, Update who & when

When you say neither works, does it mean you get an error ?

If so, please post the message

Cheers
Sathyaram

Quote:
Originally posted by janrune
Hi...
I have a simple question for one who is experienced...
In all our tables we have the two fields Edit_Who and Edit_When:
KL_Edit_Who VarChar(30) DEFAULT USER,
KL_Edit_When TIMESTAMP DEFAULT CURRENT TIMESTAMP,

In order to get those updated on each update we have to revert to triggers.
So I wrote two versions neither works...
CREATE TRIGGER KL_Key_LastGen_Upd
AFTER INSERT ON DBO.KL_Key_LastGen
FOR EACH ROW MODE DB2SQL
UPDATE DBO.KL_Key_LastGen SET KL_Edit_Who = USER, KL_Edit_When = CURRENT TIMESTAMP

CREATE TRIGGER KL_Key_LastGen_Upd
AFTER INSERT ON DBO.KL_Key_LastGen
REFERENCING NEW_TABLE AS NEWEMPS
FOR EACH STATEMENT MODE DB2SQL
UPDATE DBO.KL_Key_LastGen K SET KL_Edit_Who = USER, KL_Edit_When = CURRENT TIMESTAMP
WHERE KL_Table_Key IN( SELECT NEWEMPS.KL_Table_Key FROM NEWEMPS)

Anyone care to shed some light on my errors ???
Thanks
JR
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 08-19-03, 23:21
janrune janrune is offline
Registered User
 
Join Date: Aug 2003
Location: Austin, Texas
Posts: 12
Re: Simple trigger help needed, Update who & when

Quote:
Originally posted by sathyaram_s
When you say neither works, does it mean you get an error ?

If so, please post the message

Cheers
Sathyaram

Actually Nothing happens...
It accepts the trigger, but nothing get's executed.
(even though the first one could have updated the whole table.)
That's what's really got me stomped.
Thanks
JR
__________________
JR Andreassen
janrune_NOSPAM@io.com
Reply With Quote
  #4 (permalink)  
Old 08-20-03, 02:50
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 3,935
Re: Simple trigger help needed, Update who & when

Are you sure the trigger is valid ?

Check the VALID column in SYSCAT.TRIGGERS

Sathyaram


Quote:
Originally posted by janrune
Hi...
I have a simple question for one who is experienced...
In all our tables we have the two fields Edit_Who and Edit_When:
KL_Edit_Who VarChar(30) DEFAULT USER,
KL_Edit_When TIMESTAMP DEFAULT CURRENT TIMESTAMP,

In order to get those updated on each update we have to revert to triggers.
So I wrote two versions neither works...
CREATE TRIGGER KL_Key_LastGen_Upd
AFTER INSERT ON DBO.KL_Key_LastGen
FOR EACH ROW MODE DB2SQL
UPDATE DBO.KL_Key_LastGen SET KL_Edit_Who = USER, KL_Edit_When = CURRENT TIMESTAMP

CREATE TRIGGER KL_Key_LastGen_Upd
AFTER INSERT ON DBO.KL_Key_LastGen
REFERENCING NEW_TABLE AS NEWEMPS
FOR EACH STATEMENT MODE DB2SQL
UPDATE DBO.KL_Key_LastGen K SET KL_Edit_Who = USER, KL_Edit_When = CURRENT TIMESTAMP
WHERE KL_Table_Key IN( SELECT NEWEMPS.KL_Table_Key FROM NEWEMPS)

Anyone care to shed some light on my errors ???
Thanks
JR
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #5 (permalink)  
Old 08-20-03, 10:51
lelle12 lelle12 is offline
Registered User
 
Join Date: Mar 2003
Posts: 19
I would try something like:

CREATE TABLE KL_Key_LastGen (
KL_Edit_Who varchar(50),
KL_Edit_When timestamp,
xxx int
)@


CREATE TRIGGER KL_Key_LastGen_Upd
NO CASCADE BEFORE INSERT ON DBO.KL_Key_LastGen
REFERENCING NEW AS NEWROW
FOR EACH ROW MODE DB2SQL
set newrow.KL_Edit_When = current timestamp, KL_Edit_Who = USER
@

-- test

db2 -td@ -f dd.sql
db2 "select * from KL_Key_LastGen"

KL_EDIT_WHO KL_EDIT_WHEN XXX
-------------------------------------------------- -------------------------- -----------

0 record(s) selected.

[jon@lillebror database]$ db2 "insert into KL_Key_LastGen (xxx) values 1"
DB20000I The SQL command completed successfully.
[jon@lillebror database]$ db2 "select * from KL_Key_LastGen"

KL_EDIT_WHO KL_EDIT_WHEN XXX
-------------------------------------------------- -------------------------- -----------
JON 2003-08-20-15.47.45.490237 1

1 record(s) selected.

You will probably want to capture updates on the table, the same way. I.e. by creating triggers like

NO CASCADE BEFORE UPDATE ON DBO.KL_Key_LastGen

as well


HTH
/Lennart
Reply With Quote
  #6 (permalink)  
Old 08-20-03, 12:31
lelle12 lelle12 is offline
Registered User
 
Join Date: Mar 2003
Posts: 19
Sorry, forgot the explanation. The reason that you dont discover any action from the trigger, might depend on the fact that the rows that match the trigger condition have not yet been commited. I.e. if you construct a trigger as:

CREATE TRIGGER TEST
AFTER INSERT ON XX
REFERENCING NEW AS NEWROW
FOR EACH ROW MODE DB2SQL
UPDATE XX SET T = CURRENT TIMESTAMP
WHERE X = NEWROW.X

and then insert into an empty table

insert into xx (x) values 1

At the time of update the newrow is not yet commited, hence there are no rows in the tables that match X = NEWROW.X

Normally one uses BEFORE triggers for this type of thing. Another thing where one uses BEFORE triggers is for CONSTRAINT things (which are not possible to express as table constraints. Example:

CREATE TRIGGER CHK_WHATEVER
NO CASCADE BEFORE UPDATE ON XX
REFERENCING OLD AS OLDROW NEW AS NEWROW
FOR EACH ROW MODE DB2SQL
WHEN (
oldrow.status = 3 and newrow.status NOT IN (4, 5)
) SIGNAL SQLSTATE '75000' ('Message goes here')@


HTH
/Lennart
Reply With Quote
  #7 (permalink)  
Old 08-20-03, 12:39
janrune janrune is offline
Registered User
 
Join Date: Aug 2003
Location: Austin, Texas
Posts: 12
Re: Simple trigger help needed, Update who & when

Quote:
Originally posted by sathyaram_s
Are you sure the trigger is valid ?

Check the VALID column in SYSCAT.TRIGGERS

Sathyaram
It is listed as valid.
__________________
JR Andreassen
janrune_NOSPAM@io.com
Reply With Quote
  #8 (permalink)  
Old 08-21-03, 18:56
janrune janrune is offline
Registered User
 
Join Date: Aug 2003
Location: Austin, Texas
Posts: 12
Quote:
Originally posted by lelle12
Sorry, forgot the explanation. The reason that you dont discover any action from the trigger, might depend on the fact that the rows that match the trigger condition have not yet been commited. I.e. if you construct a trigger as:
...
HTH
/Lennart
Thanks for answering....
It still doesn't work....
I'm thinking there has to be somethingelse wrong.
Could it be trying to compile the triggers like it does with
SQL language stored procedures ???
We're having problems with that as well since we don't have a compiler or it's somehow not been installed ???

On a separate note.... Do we have to purchase the compiler separate ???
Thanks.
JR
__________________
JR Andreassen
janrune_NOSPAM@io.com
Reply With Quote
  #9 (permalink)  
Old 08-22-03, 16:17
lelle12 lelle12 is offline
Registered User
 
Join Date: Mar 2003
Posts: 19
>Thanks for answering....
>It still doesn't work....

I take it that you have changed to a before trigger and it still doesnt update those columns. What happens if you try it in a dummie schema. I.e.

CREATE TABLE Dummie.KL_Key_LastGen (
KL_Edit_Who varchar(50),
KL_Edit_When timestamp,
xxx int
)@


CREATE TRIGGER Dummie.KL_Key_LastGen_Upd
NO CASCADE BEFORE INSERT ON Dummie.KL_Key_LastGen
REFERENCING NEW AS NEWROW
FOR EACH ROW MODE DB2SQL
set newrow.KL_Edit_When = current timestamp, KL_Edit_Who = USER

and then insert a row like I did. Do you get the expected result or does it still not work?

>I'm thinking there has to be somethingelse wrong.
>Could it be trying to compile the triggers like it does with
>SQL language stored procedures ???

That should not be the case

>We're having problems with that as well since we don't have a >compiler or it's somehow not been installed ???
>
>On a separate note.... Do we have to purchase the compiler >separate ???

I dont think that there is a compiler shipped with DB2. I didnt see what platform your on, but gcc is availible for free on most platforms

/Lennart


>Thanks.
>JR
Reply With Quote
  #10 (permalink)  
Old 08-22-03, 17:13
janrune janrune is offline
Registered User
 
Join Date: Aug 2003
Location: Austin, Texas
Posts: 12
Quote:
Originally posted by lelle12
and then insert a row like I did. Do you get the expected result or does it still not work?

/Lennart
That works with the dummy table.
But still not with the real one ???
JR
__________________
JR Andreassen
janrune_NOSPAM@io.com
Reply With Quote
  #11 (permalink)  
Old 08-22-03, 17:22
fprose fprose is offline
Curmudgeon
 
Join Date: Apr 2003
Location: Phoenix, AZ
Posts: 177
Needs to be BEFORE INSERT trigger, like the test example you tried.
__________________
Fred Prose
Reply With Quote
  #12 (permalink)  
Old 08-22-03, 18:32
janrune janrune is offline
Registered User
 
Join Date: Aug 2003
Location: Austin, Texas
Posts: 12
Quote:
Originally posted by fprose
Needs to be BEFORE INSERT trigger, like the test example you tried.
It is defined as
__________________
JR Andreassen
janrune_NOSPAM@io.com
Reply With Quote
  #13 (permalink)  
Old 08-22-03, 18:34
janrune janrune is offline
Registered User
 
Join Date: Aug 2003
Location: Austin, Texas
Posts: 12
Quote:
Originally posted by janrune
It is defined as
Oops....

It is defined as :

CREATE TRIGGER DBO.KT_Key_Table_Upd
NO CASCADE BEFORE INSERT ON DBO.KT_Key_Table
REFERENCING NEW AS NEWROW
FOR EACH ROW MODE DB2SQL
set newrow.KT_Edit_When = current timestamp, newrow.KT_Edit_Who = USER

It doesn't work on the existing table, but it works just fine on
a new one.. ???
That's strange...
Is there a way to make sure there is no trigger besides "DROP TRIGGER xxxx" ???
Thanks
JR
__________________
JR Andreassen
janrune_NOSPAM@io.com
Reply With Quote
  #14 (permalink)  
Old 08-23-03, 05:26
lelle12 lelle12 is offline
Registered User
 
Join Date: Mar 2003
Posts: 19
Quote:
Originally posted by janrune
Oops....

Is there a way to make sure there is no trigger besides "DROP TRIGGER xxxx" ???
Thanks
JR
Not sure what you mean. To investigate if there are triggers, you can select from syscat.triggers. I.e.

select * from syscat.triggers where trigname = 'KT_Key_Table_Upd'

But inorder to get rid of a trigger you use drop. I.e.

drop trigger DBO.KT_Key_Table_Upd

BTW, could you post ddl for the table and trigger?


/Lennart
Reply With Quote
  #15 (permalink)  
Old 08-25-03, 12:29
janrune janrune is offline
Registered User
 
Join Date: Aug 2003
Location: Austin, Texas
Posts: 12
Quote:
Originally posted by lelle12
Not sure what you mean. To investigate if there are triggers, you can select from syscat.triggers. I.e.

select * from syscat.triggers where trigname = 'KT_Key_Table_Upd'
But inorder to get rid of a trigger you use drop. I.e.
drop trigger DBO.KT_Key_Table_Upd
BTW, could you post ddl for the table and trigger?

/Lennart
Here are the Definitions:

-- SELECT * FROM SYSCAT.TRIGGERS
-- * DROP TABLE DBO.KL_Key_LastGen
CREATE TABLE DBO.KL_Key_LastGen (
KL_Table_Key VARCHAR(18) NOT NULL,
KL_Last_NKey INT DEFAULT 1 NOT NULL,
KL_Last_CKey VARCHAR(20) DEFAULT '1' NOT NULL,
KL_DayStamp SMALLINT DEFAULT 0 NOT NULL,
KL_Edit_Who VarChar(30) DEFAULT USER,
KL_Edit_When TIMESTAMP DEFAULT CURRENT TIMESTAMP,
CONSTRAINT PK_KL_Key_LastGen PRIMARY KEY (KL_Table_Key)
)
-- DROP TRIGGER DBO.KL_Key_LastGen_Upd
CREATE TRIGGER DBO.KL_Key_LastGen_Upd
NO CASCADE BEFORE INSERT ON DBO.KL_Key_LastGen
REFERENCING NEW AS NEWROW
FOR EACH ROW MODE DB2SQL
set newrow.KL_Edit_When = current timestamp, KL_Edit_Who = USER
__________________
JR Andreassen
janrune_NOSPAM@io.com
Reply With Quote
Reply

Thread Tools
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