Monday, August 02, 2010

SQL Server App_name() function - find out which application has modified your data

Sometimes back, my friend told me that they had an application where users update their timesheets. He suspected that some users who had access to the database directly update some entries. He wanted to know whether some users update entries using a query analyser and not using the application. You can use a profiler for this.
Another method is to use app_name() function. App_name() function is used to return the name of the application from which data come. Consider the following example
declare @t table(
userid int,
timein datetime,
timeout datetime,
source varchar(200)
default app_name()
)
insert into @t(userid,timein,timeout)
select 1001,'20100710 08:30:20','20100710 18:45:00'
select * from @t
The result is:
userid timein              timeout             source
------ ------------------- ------------------- ------------------------
1001   2010-07-10 08:30:20 2010-07-10 18:45:00 Microsoft SQL Server
                                              Management Studio - Query
Read more: Beyond Relational