473,221 Members | 2,076 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,221 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 7210
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.