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 10 39501
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.DataCo l1
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCo l1 <> deleted.DataCol 1
INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol , 'DataCol2', inserted.DataCo l1
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCo l2 <> deleted.DataCol 2
OR (inserted.DataC ol2 IS NULL AND deleted.DataCol 2 IS NOT NULL)
OR (inserted.DataC ol2 IS NOT NULL AND deleted.DataCol 2 IS NULL)
INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol , 'DataCol3', inserted.DataCo l3
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCo l3 <> deleted.DataCol 3
go
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)
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.DataCo l1
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCo l1 <> deleted.DataCol 1
INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol , 'DataCol2', inserted.DataCo l1
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCo l2 <> deleted.DataCol 2
OR (inserted.DataC ol2 IS NULL AND deleted.DataCol 2 IS NOT NULL)
OR (inserted.DataC ol2 IS NOT NULL AND deleted.DataCol 2 IS NULL)
INSERT Quantrell (KeyCol, ColName, NewData)
SELECT inserted.KeyCol , 'DataCol3', inserted.DataCo l3
FROM inserted
INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol
WHERE inserted.DataCo l3 <> deleted.DataCol 3
go
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)
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_rFa ct.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.DataCo l1 FROM inserted INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol WHERE inserted.DataCo l1 <> deleted.DataCol 1
INSERT Quantrell (KeyCol, ColName, NewData) SELECT inserted.KeyCol , 'DataCol2', inserted.DataCo l1 FROM inserted INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol WHERE inserted.DataCo l2 <> deleted.DataCol 2 OR (inserted.DataC ol2 IS NULL AND deleted.DataCol 2 IS NOT NULL) OR (inserted.DataC ol2 IS NOT NULL AND deleted.DataCol 2 IS NULL)
INSERT Quantrell (KeyCol, ColName, NewData) SELECT inserted.KeyCol , 'DataCol3', inserted.DataCo l3 FROM inserted INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol WHERE inserted.DataCo l3 <> deleted.DataCol 3 go
Best, Hugo
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)
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_rFa ct.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.DataCo l1 FROM inserted INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol WHERE inserted.DataCo l1 <> deleted.DataCol 1
INSERT Quantrell (KeyCol, ColName, NewData) SELECT inserted.KeyCol , 'DataCol2', inserted.DataCo l1 FROM inserted INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol WHERE inserted.DataCo l2 <> deleted.DataCol 2 OR (inserted.DataC ol2 IS NULL AND deleted.DataCol 2 IS NOT NULL) OR (inserted.DataC ol2 IS NOT NULL AND deleted.DataCol 2 IS NULL)
INSERT Quantrell (KeyCol, ColName, NewData) SELECT inserted.KeyCol , 'DataCol3', inserted.DataCo l3 FROM inserted INNER JOIN deleted ON deleted.KeyCol = inserted.KeyCol WHERE inserted.DataCo l3 <> deleted.DataCol 3 go
Best, Hugo
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)
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), databeforechang e (?), dataafterchange (?), userID int,
dateofchange smalldatetime.
This must be a pretty common need?
lq
Hugo Kornelis <hugo@pe_NO_rFa ct.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
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) , databeforechang e (?), 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)
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), databeforechang e (?), dataafterchange (?), userID int,
dateofchange smalldatetime.
This must be a pretty common need?
lq
Hugo Kornelis <hugo@pe_NO_rFa ct.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 function will be to
INSERT a row into my MasterChanges table everytime ANY data is changed
in each of the 5 tables.
I have set up MasterChanges to capture the following:
what Table the change was made in
|
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
getting the column name programatically. This is all working fine.
If I do a regular insert command in my trigger then everything works
fine. However, since I want to retrieve data from the column name
which I got programatically from the...
|
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 update on the row that was changed
when the trigger was fired.
To explain, I have 2 columns, one which contains a member number, the
|
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 values that are
passed in via the insert trigger without having to use all the 'set'
statements for each field (so if we add fields in the future I won't
have to update the trigger). In other words, I want the trigger code
to look something like...
|
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 Table-A: Col1, Col2, Col3, Col4,Col5
| |
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 value for each field. I need to make an
audit table that must contain only the fields changed after and
insert/update.
Thanks in advance
|
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 AFTER trigger), I can see two
approaches:
a) Write a separate PL/pgSQL function for each table, with the hard
coded field names in the function.
|
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 schema1.emp (
fname varchar(15) not null,
lname varchar(15) not null,
dob date,
|
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 well old value in table B. For ex:
Table B
PK Oldvalue NewValue
1 Brown Blonde
I don't want to insert all columns values instead I want to know what specific column has been changed. if two values are...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |