Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server : get at the name of the app causing an update in a trigger

We're trying to pinpoint the source of some unexpected updates happening on a SQL Server table. What I'd like to do is create a trigger on that table monitoring that column we're interested in, and when updates occur, write some audit info into a separate table.

Works great and fine for things like user name, date, old and new columns values (from the Inserted and Deleted trigger tables) - but I'd like more :-)

In SQL Profiler, you can see the name of the app that's connected to SQL Server in the profiles - the part that can be defined in the connection string as Application Name:

Data Source=(local);Initial Catalog=AdventureWorks;
Integrated Security=True;Application Name="My Application"

Is there any way in a T-SQL FOR UPDATE trigger to get at this information?

like image 350
marc_s Avatar asked Mar 17 '11 15:03

marc_s


People also ask

How do I know which column is updated in a trigger?

SQL Server COLUMNS_UPDATED() Function for Triggers. This function is used to know the inserted or updated columns of a table or view. It returns a VARBINARY stream that by using a bitmask allows you to test for multiple columns.

Can we use update in trigger?

AFTER UPDATE Trigger in SQL is a stored procedure on a database table that gets invoked or triggered automatically after an UPDATE operation gets successfully executed on the specified table. For uninitiated, the UPDATE statement is used to modify data in existing rows of a data table.

How check triggers in SQL Server?

To view database level triggers, Login to the server using SQL Server management studio and navigate to the database. Expand the database and navigate to Programmability -> Database Triggers. To view triggers at the server level, Login to Server using SSMS and navigate to Server Objects and then Triggers folder.

What is a CLR trigger?

A trigger is a special type of stored procedure that automatically runs when a language event executes. Because of the Microsoft SQL Server integration with the . NET Framework common language runtime (CLR), you can use any . NET Framework language to create CLR triggers.


2 Answers

SELECT APP_NAME()

like image 117
Martin Smith Avatar answered Oct 26 '22 23:10

Martin Smith


Marc_S the sP_whoIsActive stored Procedure probably will solve Your problems as posted here

and it's awesome part of T-SQL by the way

like image 42
luckyluke Avatar answered Oct 27 '22 00:10

luckyluke