473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SQL Trigger for changed columns?

I have never written a trigger before and now am seeing the light. Is
there a way to write a trigger so that if a user changes any column in
a single row on one table then the trigger will write the value of
this (these) rows to a second table. I don't want the unchanged
columns, just the changed columns (with column names...)
Thanks,
lq
Jul 20 '05 #1
10 39463
On 12 Apr 2004 18:36:24 -0700, Lauren Quantrell wrote:
I have never written a trigger before and now am seeing the light. Is
there a way to write a trigger so that if a user changes any column in
a single row on one table then the trigger will write the value of
this (these) rows to a second table. I don't want the unchanged
columns, just the changed columns (with column names...)
Thanks,
lq


Something like this, maybe?

CREATE TABLE Lauren
(KeyCol int not null primary key,
DataCol1 int not null,
DataCol2 int null,
DataCol3 int not null)
go
CREATE TABLE Quantrell
(KeyCol int not null primary key,
ColName char(8) not null,
NewData int not null)
go

CREATE TRIGGER LaurenQuantrell
ON Lauren AFTER UPDATE
AS
IF UPDATE(KeyCol)
BEGIN
RAISERROR ('Don't change the key column!!', 16, 1)
ROLLBACK TRANSACTION
RETURN
END

INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol, 'DataCol1', inserted.DataCol1
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCol1 <> deleted.DataCol1

INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol, 'DataCol2', inserted.DataCol1
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCol2 <> deleted.DataCol2
OR (inserted.DataCol2 IS NULL AND deleted.DataCol2 IS NOT NULL)
OR (inserted.DataCol2 IS NOT NULL AND deleted.DataCol2 IS NULL)

INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol, 'DataCol3', inserted.DataCol3
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCol3 <> deleted.DataCol3
go
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2
On 12 Apr 2004 18:36:24 -0700, Lauren Quantrell wrote:
I have never written a trigger before and now am seeing the light. Is
there a way to write a trigger so that if a user changes any column in
a single row on one table then the trigger will write the value of
this (these) rows to a second table. I don't want the unchanged
columns, just the changed columns (with column names...)
Thanks,
lq


Something like this, maybe?

CREATE TABLE Lauren
(KeyCol int not null primary key,
DataCol1 int not null,
DataCol2 int null,
DataCol3 int not null)
go
CREATE TABLE Quantrell
(KeyCol int not null primary key,
ColName char(8) not null,
NewData int not null)
go

CREATE TRIGGER LaurenQuantrell
ON Lauren AFTER UPDATE
AS
IF UPDATE(KeyCol)
BEGIN
RAISERROR ('Don't change the key column!!', 16, 1)
ROLLBACK TRANSACTION
RETURN
END

INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol, 'DataCol1', inserted.DataCol1
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCol1 <> deleted.DataCol1

INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol, 'DataCol2', inserted.DataCol1
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCol2 <> deleted.DataCol2
OR (inserted.DataCol2 IS NULL AND deleted.DataCol2 IS NOT NULL)
OR (inserted.DataCol2 IS NOT NULL AND deleted.DataCol2 IS NULL)

INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol, 'DataCol3', inserted.DataCol3
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCol3 <> deleted.DataCol3
go
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #3
Hugo,
Thanks for that code!
Now, the rub is that all the columns are integer types. I've got
ntext, nvarchar, int, smallint, tinyint...
lq

Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<e6********************************@4ax.com>. ..
On 12 Apr 2004 18:36:24 -0700, Lauren Quantrell wrote:
I have never written a trigger before and now am seeing the light. Is
there a way to write a trigger so that if a user changes any column in
a single row on one table then the trigger will write the value of
this (these) rows to a second table. I don't want the unchanged
columns, just the changed columns (with column names...)
Thanks,
lq


Something like this, maybe?

CREATE TABLE Lauren
(KeyCol int not null primary key,
DataCol1 int not null,
DataCol2 int null,
DataCol3 int not null)
go
CREATE TABLE Quantrell
(KeyCol int not null primary key,
ColName char(8) not null,
NewData int not null)
go

CREATE TRIGGER LaurenQuantrell
ON Lauren AFTER UPDATE
AS
IF UPDATE(KeyCol)
BEGIN
RAISERROR ('Don't change the key column!!', 16, 1)
ROLLBACK TRANSACTION
RETURN
END

INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol, 'DataCol1', inserted.DataCol1
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCol1 <> deleted.DataCol1

INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol, 'DataCol2', inserted.DataCol1
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCol2 <> deleted.DataCol2
OR (inserted.DataCol2 IS NULL AND deleted.DataCol2 IS NOT NULL)
OR (inserted.DataCol2 IS NOT NULL AND deleted.DataCol2 IS NULL)

INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol, 'DataCol3', inserted.DataCol3
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCol3 <> deleted.DataCol3
go
Best, Hugo

Jul 20 '05 #4
On 14 Apr 2004 16:13:37 -0700, Lauren Quantrell wrote:
Hugo,
Thanks for that code!
Now, the rub is that all the columns are integer types. I've got
ntext, nvarchar, int, smallint, tinyint...
lq


Hi Lauren,

If you need the data in the second table for audit purposes, then this
might be one of the very rare cases where datatype sql_variant can be
used. Check Books Online for details on this datatype and see if it
can be used in your situation.

I the second table is not for audit, then it might well be worth to
consider the design of this table. If you can explain what business
problem you have to solve, I can try if I can find a better solution.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #5
Hugo,
Thanks for that code!
Now, the rub is that all the columns are integer types. I've got
ntext, nvarchar, int, smallint, tinyint...
lq

Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<e6********************************@4ax.com>. ..
On 12 Apr 2004 18:36:24 -0700, Lauren Quantrell wrote:
I have never written a trigger before and now am seeing the light. Is
there a way to write a trigger so that if a user changes any column in
a single row on one table then the trigger will write the value of
this (these) rows to a second table. I don't want the unchanged
columns, just the changed columns (with column names...)
Thanks,
lq


Something like this, maybe?

CREATE TABLE Lauren
(KeyCol int not null primary key,
DataCol1 int not null,
DataCol2 int null,
DataCol3 int not null)
go
CREATE TABLE Quantrell
(KeyCol int not null primary key,
ColName char(8) not null,
NewData int not null)
go

CREATE TRIGGER LaurenQuantrell
ON Lauren AFTER UPDATE
AS
IF UPDATE(KeyCol)
BEGIN
RAISERROR ('Don't change the key column!!', 16, 1)
ROLLBACK TRANSACTION
RETURN
END

INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol, 'DataCol1', inserted.DataCol1
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCol1 <> deleted.DataCol1

INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol, 'DataCol2', inserted.DataCol1
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCol2 <> deleted.DataCol2
OR (inserted.DataCol2 IS NULL AND deleted.DataCol2 IS NOT NULL)
OR (inserted.DataCol2 IS NOT NULL AND deleted.DataCol2 IS NULL)

INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol, 'DataCol3', inserted.DataCol3
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCol3 <> deleted.DataCol3
go
Best, Hugo

Jul 20 '05 #6
On 14 Apr 2004 16:13:37 -0700, Lauren Quantrell wrote:
Hugo,
Thanks for that code!
Now, the rub is that all the columns are integer types. I've got
ntext, nvarchar, int, smallint, tinyint...
lq


Hi Lauren,

If you need the data in the second table for audit purposes, then this
might be one of the very rare cases where datatype sql_variant can be
used. Check Books Online for details on this datatype and see if it
can be used in your situation.

I the second table is not for audit, then it might well be worth to
consider the design of this table. If you can explain what business
problem you have to solve, I can try if I can find a better solution.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #7
Hugo,
Thanks for your replies and your help.
I'm trying to keep a record of every change a user makes to any field
on a form.
The underlying recordsource is an SP that refers to a single table.
The user can only modify one row at a time.

Ideally, I would track changes to datetime, int, nvarchar and ntext
columns.
This is used entirely as an audit trail, but users would be able to
click a button to view the changes any user had made.

The table holding the audit would need a column for the fieldname
nvarchar(25), databeforechange (?), dataafterchange (?), userID int,
dateofchange smalldatetime.

This must be a pretty common need?

lq

Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<pn********************************@4ax.com>. ..
On 14 Apr 2004 16:13:37 -0700, Lauren Quantrell wrote:
Hugo,
Thanks for that code!
Now, the rub is that all the columns are integer types. I've got
ntext, nvarchar, int, smallint, tinyint...
lq


Hi Lauren,

If you need the data in the second table for audit purposes, then this
might be one of the very rare cases where datatype sql_variant can be
used. Check Books Online for details on this datatype and see if it
can be used in your situation.

I the second table is not for audit, then it might well be worth to
consider the design of this table. If you can explain what business
problem you have to solve, I can try if I can find a better solution.

Best, Hugo

Jul 20 '05 #8
On 15 Apr 2004 15:49:24 -0700, Lauren Quantrell wrote:
Hugo,
Thanks for your replies and your help.
I'm trying to keep a record of every change a user makes to any field
on a form.
The underlying recordsource is an SP that refers to a single table.
The user can only modify one row at a time.

Ideally, I would track changes to datetime, int, nvarchar and ntext
columns.
This is used entirely as an audit trail, but users would be able to
click a button to view the changes any user had made.

The table holding the audit would need a column for the fieldname
nvarchar(25), databeforechange (?), dataafterchange (?), userID int,
dateofchange smalldatetime.

This must be a pretty common need?

lq


Hi Lauren,

The need is pretty common indeed. But you have chosen an approach that
differs from how I normally solve this.

I prefer to make an audit table with the same rows as the actual data
table (but all except the key columns nullable), plus extra rows (not
nullable) for userid, time of change, etc. PK of this table is equal
to the PK of the actual data table plus time of change.

Then, I make the following triggers:
* One trigger for insert, update that enters all "new" information
(from the inserted pseudo-table) into the audit table
* One trigger for delete that enters just the key columns into the
audit table. If all non-key columns in the actual data table are
nullable, you'll also need a "deleted" indication to see the
difference between a deletion and an update that sets all non-key
columns to null.

Advantage of this approach is that you have all information at a
glance, the triggers are easy to set up and will also work when
set-based changes are carried out on the data and it saves some space
if changes usually affect multiple columns at the same time.

Disadvantage is that your approach costs less space if changes usually
affect only one or two columns. That being said, I'd still choose "my"
approach even if it costs more disk space - I value ease of use and
maintainability more than disk space.

(Note - the ntext columns might require special handling, in both
scenario's. In "your" version because a sql_variant datatype can't be
used to hold ntext (if I recall correctly); in "my" version because it
might be worthwile to exclude this column from the audit table if the
text is rarely changed. Besides, I don't know how ntext data is
handled in triggers and if all set-based operations are allowed on
ntext data)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #9
Hugo,
Thanks for your replies and your help.
I'm trying to keep a record of every change a user makes to any field
on a form.
The underlying recordsource is an SP that refers to a single table.
The user can only modify one row at a time.

Ideally, I would track changes to datetime, int, nvarchar and ntext
columns.
This is used entirely as an audit trail, but users would be able to
click a button to view the changes any user had made.

The table holding the audit would need a column for the fieldname
nvarchar(25), databeforechange (?), dataafterchange (?), userID int,
dateofchange smalldatetime.

This must be a pretty common need?

lq

Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<pn********************************@4ax.com>. ..
On 14 Apr 2004 16:13:37 -0700, Lauren Quantrell wrote:
Hugo,
Thanks for that code!
Now, the rub is that all the columns are integer types. I've got
ntext, nvarchar, int, smallint, tinyint...
lq


Hi Lauren,

If you need the data in the second table for audit purposes, then this
might be one of the very rare cases where datatype sql_variant can be
used. Check Books Online for details on this datatype and see if it
can be used in your situation.

I the second table is not for audit, then it might well be worth to
consider the design of this table. If you can explain what business
problem you have to solve, I can try if I can find a better solution.

Best, Hugo

Jul 20 '05 #10
On 15 Apr 2004 15:49:24 -0700, Lauren Quantrell wrote:
Hugo,
Thanks for your replies and your help.
I'm trying to keep a record of every change a user makes to any field
on a form.
The underlying recordsource is an SP that refers to a single table.
The user can only modify one row at a time.

Ideally, I would track changes to datetime, int, nvarchar and ntext
columns.
This is used entirely as an audit trail, but users would be able to
click a button to view the changes any user had made.

The table holding the audit would need a column for the fieldname
nvarchar(25), databeforechange (?), dataafterchange (?), userID int,
dateofchange smalldatetime.

This must be a pretty common need?

lq


Hi Lauren,

The need is pretty common indeed. But you have chosen an approach that
differs from how I normally solve this.

I prefer to make an audit table with the same rows as the actual data
table (but all except the key columns nullable), plus extra rows (not
nullable) for userid, time of change, etc. PK of this table is equal
to the PK of the actual data table plus time of change.

Then, I make the following triggers:
* One trigger for insert, update that enters all "new" information
(from the inserted pseudo-table) into the audit table
* One trigger for delete that enters just the key columns into the
audit table. If all non-key columns in the actual data table are
nullable, you'll also need a "deleted" indication to see the
difference between a deletion and an update that sets all non-key
columns to null.

Advantage of this approach is that you have all information at a
glance, the triggers are easy to set up and will also work when
set-based changes are carried out on the data and it saves some space
if changes usually affect multiple columns at the same time.

Disadvantage is that your approach costs less space if changes usually
affect only one or two columns. That being said, I'd still choose "my"
approach even if it costs more disk space - I value ease of use and
maintainability more than disk space.

(Note - the ntext columns might require special handling, in both
scenario's. In "your" version because a sql_variant datatype can't be
used to hold ntext (if I recall correctly); in "my" version because it
might be worthwile to exclude this column from the audit table if the
text is rarely changed. Besides, I don't know how ntext data is
handled in triggers and if all set-based operations are allowed on
ntext data)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Trevor Fairchild | last post by:
I am trying to create a very minimal auditing system for a series of databases. I am in the process of writing Update triggers for 5 Tablse. I will write a trigger for each table-the trigger's...
1
by: Rebecca Lovelace | last post by:
I have a trigger on a table. I am trying to dynamically log the changed fields on the table to another table, so I am iterating through the bits in COLUMNS_UPDATED() to find what's changed, and...
2
by: M Wells | last post by:
Hi All, I'm a relatively newbie to SQL Server 2000, having come from a MySQL background. I'm creating my first Trigger statement on a table, and I'd like to know how I go about performing an...
3
by: takilroy | last post by:
Hi, Does anyone know of a simple way to do this? I want to create an insert trigger for a table and if the record already exists based on some criteria, I want to update the table with the...
1
by: Tolga Yaramis via SQLMonster.com | last post by:
Hi All I have a question about generating dynmamicly If Update() statement in a trigger.. in My db, there is a table that holds some column names of an another table. for example; Columns...
4
by: Josué Maldonado | last post by:
Hello list, First of all, excuse me if this is not the right place to ask my question. Is there a way in postgresql to loop to all the fields of a given table and compare the OLD and NEW...
3
by: Justin Clift | last post by:
Hi all, I'm creating a centralised table to keep a log of changes in other tables. In thinking about the PL/pgSQL trigger to write and attach to the monitored tables (probably a row level...
5
by: wpellett | last post by:
I can not get the SQL compiler to rewrite my SQL UPDATE statement to include columns being SET in a Stored Procedure being called from a BEFORE UPDATE trigger. Example: create table...
1
parshupooja
by: parshupooja | last post by:
Hey All, I have table A which has 20 columns. I want to create trigger on table A which should get triggered whenever someone tries to update any value in Table A than trigger should insert new as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.