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 Search this Thread Display Modes
  #16 (permalink)  
Old 08-25-03, 12:30
lelle12 lelle12 is offline
Registered User
 
Join Date: Mar 2003
Posts: 82
Quote:
Originally posted by janrune
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
I have no idea why it doesnt work out for you. Here is what I did, and it works perfectly (I think, do you agree that my results are correct?)
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
@

insert into DBO.KL_Key_LastGen
(KL_Table_Key, KL_Last_NKey, KL_Last_CKey, KL_DayStamp)
values ('My Test Key 1', 1, 'My Test CKey 1', 1),
('My Test Key 2', 2, 'My Test CKey 2', 2)@

KL_TABLE_KEY KL_LAST_NKEY KL_LAST_CKEY KL_DAYSTAMP KL_EDIT_WHO KL_EDIT_WHEN
------------------ ------------ -------------------- ----------- ------------------------------ --------------------------
My Test Key 1 1 My Test CKey 1 1 JON 2003-08-25-18.34.28.303779
My Test Key 2 2 My Test CKey 2 2 JON 2003-08-25-18.34.28.303779

2 record(s) selected.

/Lennart


select * from DBO.KL_Key_LastGen
Reply With Quote
  #17 (permalink)  
Old 08-26-03, 15:04
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
In returning to your original note, it sounds like you need a BEFORE UPDATE trigger as well (suggest changing the BEFORE INSERT trigger name to KL_Key_LastGen_Ins):

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


Quote:
Originally posted by janrune
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
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