Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trigger at the field level

Status
Not open for further replies.

Jomercat

Programmer
Sep 1, 2004
100
US
Hi all,

I am new to the SQL triggers.

I am designing an application which uses SQL as the backend. I want to be able to track changes to five fields of a table. Is it posible to use triggers at the field level. In other words, fire the trigger if either of the five fields is updated?

Thanks in advance.

Jose.
 
SQLDenis,

Thanks for the prompt reply!

I will check the link.

Jose.
 
SQLSister,

Now If I want to log the individual changed values; in other words, field A (oldvale-newvalue), field B (oldvale-newvalue), etc. how would I write the statement. I got it to work for one field, but when I try to update more that one, I am getting an error.....

"Msg 8152, Level 16, State 14, Procedure trg_name_upt, Line 59
String or binary data would be truncated."

Here is what I am doing.

CREATE TRIGGER [trg_name_upt]
ON

FOR UPDATE AS

IF UPDATE(fieldA)

BEGIN
INSERT INTO logtable
(field1,
Field2,
SELECT del.oldvalue,
(SELECT ins.newvalue
FROM inserted ins)
FROM deleted del
END

IF UPDATE(fieldB)

BEGIN
INSERT INTO logtable
(field1,
Field2,
SELECT del.oldvalue,
(SELECT ins.newvalue
FROM inserted ins)
FROM deleted del
END

IF UPDATE(fieldC)

BEGIN
INSERT INTO logtable
(field1,
Field2,
SELECT del.oldvalue,
(SELECT ins.newvalue
FROM inserted ins)
FROM deleted del
END

Thanks in advance.

Jose.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top