views:

69

answers:

3

How do I find out what application or SP is modifing the values in a config table? I thought I had isolated the app that was responsible but these particular values keep chnging back to true when I keep modifying them to be false.

+5  A: 

First, create a logging table:

CREATE TABLE modlog(
   datestamp smalldatetime,
   username varchar(255) NOT NULL DEFAULT SYSTEM_USER
   );

Then create an UPDATE trigger on your table:

CREATE TRIGGER mytable_mods ON mytable FOR UPDATE AS
   INSERT INTO modlog(smalldatetime) VALUES (GETDATE());

Just peek into the modlog table to figure out which user is updating the table, and when. You could get fancy and also log particular fields being updated.

Another approach would be to set up a trace in SQL Server Profiler, filter the heck out of it so it only returns updates on that table, and keep it open until something happens.

If your applications include the ApplicationName parameter in their connection strings, you can use App_Name() instead of SYSTEM_USER, which will log the application name, removing the extra detective work. Knowing the user might still be useful so you can figure out what they are doing to trigger the update.

richardtallent
Will that identify the user name?
Robert Harvey
@richard, if you can correlate the user name with a specific application, this should work.
Robert Harvey
Yes, that will identify the username associated with the SQL Server session that is performing the update. Hopefully knowing the user and time will be enough to figure out which app was responsible.
richardtallent
Of course, if all your users come through an ASP.NET application which then connects to your database using a single default user for the application, then everyone is called "application user" :-(
marc_s
@marc_s The OP is ideally looking for which *app* is causing the problem, so as long as he uses the ApplicationName parameter in his connection strings for each app, it should be possible to nail down the troble-maker. Logging the user instead was a work-around in case he can't use ApplicationName. In some cases (generally, WinForms or tightly-integrated intranet apps), the "user" will be a literal Windows user. But for ASP.NET apps, it's possible his apps each have their own SQL Server login that they impersonate rather than all using one login, so maybe it'll still do the trick.
richardtallent
This answer just keeps getting better. I was looking for the app and I can modify conn strings of apps I suspect so App_Name() is really useful.
jimconstable
+1  A: 

Create a trigger to roll back the update. Wait for the app to error out. It can be a very simple trigger:

CREATE TRIGGER BugOffRogueProgram
ON MyConfigTable
FOR UPDATE
AS
BEGIN
ROLLBACK TRAN
END

Aaron Alton
A: 

The answers provided so far are absolutely on the spot - that's the way to do it in SQL Server 2005.

Just as a brief teaser: in SQL Server 2008, there's a new feature called Change Data Capture to support this exact scenario "out of the box" without the need to write triggers and update tables yourself. Quite handy!

Marc

marc_s