473,394 Members | 1,878 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,394 software developers and data experts.

Trigger with exec statement blocking execution of triggering cmd?

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 inserted and deleted tables (to
get the old and new values) I wanted to do something like this:
insert into auditTransactionLog (TableName,
PrimaryKeyId,ColumnName,OldValue, NewValue, ActionType) EXEC( 'SELECT
(''cmContactInfo''), I.contactID,'''+ @colname+''', D.'+@colname+',
I.'+@colname+', '+@action+' FROM inserted I INNER JOIN Deleted D on
I.ContactId = D.ContactId')

The presence of this line of code appears to be preventing the
updating of the table with the trigger. Is there some reason why I
can't do the EXEC in the trigger? If I did it without EXEC it works
fine but I have no idea of getting at the D. and I. @colname columns
otherwise.

Thanks for any help!
Rebecca
Jul 20 '05 #1
1 7219
Rebecca Lovelace (us********@yahoo.com) writes:
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 inserted and deleted tables (to
get the old and new values) I wanted to do something like this:
insert into auditTransactionLog (TableName,
PrimaryKeyId,ColumnName,OldValue, NewValue, ActionType) EXEC( 'SELECT
(''cmContactInfo''), I.contactID,'''+ @colname+''', D.'+@colname+',
I.'+@colname+', '+@action+' FROM inserted I INNER JOIN Deleted D on
I.ContactId = D.ContactId')

The presence of this line of code appears to be preventing the
updating of the table with the trigger. Is there some reason why I
can't do the EXEC in the trigger? If I did it without EXEC it works
fine but I have no idea of getting at the D. and I. @colname columns
otherwise.


The inserted/deleted tables are only avaliable directly in a trigger.
The dynamic SQL created by EXEC() constitutes a different scope,
akin a call to a stored procedure.

A possible work around, is to use a temp table and copy inserted/
deleted to this table.

I would however recommend you to reconsider you approach completely.
There are several problems with this approach:

o Columns_updated() only tells whether the column was assigned to in
an UPDATE statement; it does not say whether there was a change.
o Looping over columns and building dynamic SQL may take its time,
and triggers should be fast, as they execute in transaction context.

Better in to get the inserted/deleted variables into table variables,
and then have static SQL foreach column to catch changes. Yes, that
is boring code to write, but you could write a tool that generates
the triggers for you.

Or you could investigate available third-party products for auditing.
Although they do not come for free, they may be more cost-effective
in the long run.

I know of two such products SQL Audit from Red Matrix (www.red-matrix.com)
and Entegra from Lumgigent (www.lumigenet.com). SQL Audit is trigger
based, while Entegra works from a transaction log, and is likely to
be less intrusive.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

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

Similar topics

33
by: coosa | last post by:
I have a table: ---------------------------------------------------- CREATE TABLE CATEGORY ( CATEGORY_ID INTEGER IDENTITY(1,1) NOT NULL, CATEGORY_NAME VARCHAR(40) NOT NULL,...
6
by: Mary | last post by:
We are developing a DB2 V7 z/OS application which uses a "trigger" table containing numerous triggers - each of which is activated by an UPDATE to a different column of this "trigger" table. When...
6
by: JohnO | last post by:
Hi Folks, I have an update trigger that fails (it inserts an audit table record) in some circumstances. This is causing the triggering transaction to fail and roll back. Is there any way to...
0
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as...
8
by: Frank van Vugt | last post by:
Hi, If during a transaction a number of deferred triggers are fired, what will be their execution order upon the commit? Will they be executed in order of firing or alfabetically or...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
6
by: wugon.net | last post by:
Hi , Anyone know how to monitor db2 trigger activity ? We suffer some trigger issue , and we try to monitor trigger's behavior use event monitor and db2audit, but both tools can not get...
0
by: wugon.net | last post by:
Hi , Anyone know how to monitor db2 trigger activity ? We suffer some trigger issue today and we try to monitor trigger's behavior use event monitor and db2audit, but both tools can not get...
9
by: Ots | last post by:
I'm using SQL 2000, which is integrated with a VB.NET 2003 app. I have an Audit trigger that logs changes to tables. I want to apply this trigger to many different tables. It's the same trigger,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
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,...
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...

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.