x
login about faq Site discussion (meta-askssc)

Finding the user's login

If I am auditing user actions and I want to be sure that I can capture the actual login, is there a function I can use in my auditing trigger?

more ▼

asked Dec 04 '09 at 09:36 PM in Default

Steve Jones - Editor gravatar image

Steve Jones - Editor ♦♦
5.1k 74 78 82

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

There are a couple of built-in functions to give you the login for auditing.

  • suser_sname()
  • system_user

One thing to keep in mind with these is that a login with impersonate privileges can cause the trigger to record an incorrect login by changing the execution context prior to activating the trigger. Here's a quick demo query:

-- we need a user to spoof
create user bob

-- impersonate bob
execute as user = 'bob'

select suser_sname()
select system_user

-- done impersonating
revert

-- get rid of bob
drop user bob
more ▼

answered Dec 05 '09 at 01:34 AM

KenJ gravatar image

KenJ
12.4k 2 10

Exactly. Use suser_sname().

Dec 05 '09 at 04:02 AM David Wimbush
(comments are locked)
10|1200 characters needed characters left

The system_user function should give you the login of the user executing the query if it is invoked inside a trigger.

more ▼

answered Dec 04 '09 at 09:50 PM

TimothyAWiseman gravatar image

TimothyAWiseman
14.3k 16 20 29

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x145
x73
x33

asked: Dec 04 '09 at 09:36 PM

Seen: 830 times

Last Updated: Dec 04 '09 at 09:36 PM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.