dbTalk Databases Forums  

Update trigger or ?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Update trigger or ? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Joerg Gempe
 
Posts: n/a

Default Update trigger or ? - 04-23-2007 , 05:25 AM






Hello,

I've a problem where some data gets updated but I don't know which
process (SP) is responsible for it (it's an old installation which I've
taken over).
Is it somehow possible to know which process/user/SP performs an update
on a special table/column?
I was thinking about to implement an update trigger but unfortunately
I've no idea how to figure out the "parent process" which was
responsible for the update.

I'm using MS SQL Server 2000.


Thanks Joerg


Reply With Quote
  #2  
Old   
Lagbehind@googlemail.com
 
Posts: n/a

Default Re: Update trigger or ? - 04-23-2007 , 11:26 AM






On 23 Apr, 11:25, Joerg Gempe <j_spam_filter_ge... (AT) gmx (DOT) de> wrote:
Quote:
Hello,

I've a problem where some data gets updated but I don't know which
process (SP) is responsible for it (it's an old installation which I've
taken over).
Is it somehow possible to know which process/user/SP performs an update
on a special table/column?
I was thinking about to implement an update trigger but unfortunately
I've no idea how to figure out the "parent process" which was
responsible for the update.

I'm using MS SQL Server 2000.

Thanks Joerg



Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Update trigger or ? - 04-23-2007 , 04:52 PM



Joerg Gempe (j_spam_filter_gempe (AT) gmx (DOT) de) writes:
Quote:
I've a problem where some data gets updated but I don't know which
process (SP) is responsible for it (it's an old installation which I've
taken over).
Is it somehow possible to know which process/user/SP performs an update
on a special table/column?
This query:

SELECT o.name, o2.name, c.name
FROM sysobjects o
JOIN sysdepends d ON o.id = d.id
JOIN sysobjects o2 ON d.depid = o2.id
JOIN syscolumns c ON d.depid = c.id
AND d.depnumber = c.colid
WHERE o2.name = 'yourtbl'
AND c.name = 'yourcol'
AND d.resultobj = 1

may return the information you need. I say may, because the dependency
information in a database is rarely complete. This is because if you
drop and recreate a table, without reloading the stored procedures,
the depencies are lost.

If it's possible for you to build the database from scripts, and
making sure that procedures and triggers are built after all tables,
then your odds are better.

Quote:
I was thinking about to implement an update trigger but unfortunately
I've no idea how to figure out the "parent process" which was
responsible for the update.
DBCC INPUTBUFFER could address this, but:

1) it requires the user to have sysadm privileges.
2) it will only show you the command sent from the client. If procedures
nest in several levels, this information may not be sufficient.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #4  
Old   
Joerg Gempe
 
Posts: n/a

Default Re: Update trigger or ? - 04-25-2007 , 05:23 AM



Erland Sommarskog wrote:
Quote:
This query:

SELECT o.name, o2.name, c.name
FROM sysobjects o
JOIN sysdepends d ON o.id = d.id
JOIN sysobjects o2 ON d.depid = o2.id
JOIN syscolumns c ON d.depid = c.id
AND d.depnumber = c.colid
WHERE o2.name = 'yourtbl'
AND c.name = 'yourcol'
AND d.resultobj = 1
Hello,

Thanks for this, this already helps a little bit and I know now all the
SPs, but it is unfortunately not 100% what I'm was looking for.

I want to know at runtime who or what changes a value in a specific
table,column,row.
Problem is that it might not only a SP but a plain SQL statement or
another trigger or ... ?

I want to create an Update trigger to capture the old value, the new
value and which process/user/sql command performed the update.
So roughly spoken I'm looking for something like:

create trigger getCaller
on MyTable
for update
as
--- problem how to figure out the caller who's responsible that the
trigger is called
select @caller = .?.?.?.
---
insert into MyCallerTable select @caller, getdate(), * from deleted,
inserted
....


Where @caller should give me as much information as possible about the
process which "runs" the trigger.

Thank you
Joerg



Reply With Quote
  #5  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Update trigger or ? - 04-25-2007 , 10:31 AM



Joerg Gempe (j_spam_filter_gempe (AT) gmx (DOT) de) writes:
Quote:
I want to know at runtime who or what changes a value in a specific
table,column,row.
Problem is that it might not only a SP but a plain SQL statement or
another trigger or ... ?
Triggers are not a problem. They are captured by the above.

If you have applications emitting loose SQL statements without stored
procedures, you now see one reason why you should not have this. I guess
you will have to first search the code for the table name, and then
weed out the UPDATE statements.

Quote:
I want to create an Update trigger to capture the old value, the new
value and which process/user/sql command performed the update.
So roughly spoken I'm looking for something like:

create trigger getCaller
on MyTable
for update
as
--- problem how to figure out the caller who's responsible that the
trigger is called
select @caller = .?.?.?.
---
insert into MyCallerTable select @caller, getdate(), * from deleted,
inserted
...
As I said, to do this at run-time DBCC INPUTBUFFER is your only option. But
it will only work if the caller has sysadm privileges. And it's not going to
help the throughput of the application.

It's an uphill battle, I'm afraid.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.