Groups | Blog | Home
all groups > sql server (alternate) > october 2004 >

sql server (alternate) : Current user


William F. O'Neill
10/7/2004 2:59:21 PM
Am using SQL Server 2000, and am writing some audit triggers. How do I get
the the name of the user, eg. user_id or current_user. Just spent an hour
looking through the SAMS book for this, and can't find any reference.

ParrotRob
10/7/2004 11:15:30 PM
[quoted text, click to view]

select user_name()

William F. O'Neill
10/8/2004 3:09:34 AM
Thank you ParrotRob, but that gives me 'dbo.' If I log on to my
application, I want to be able to capture 'my' logon id, eg. wfoneill.
Remember, I want to be able to use this in my Insert, Update, & Delete
triggers.
Bill...

[quoted text, click to view]

Steve Troxell
10/8/2004 8:20:38 AM
[quoted text, click to view]

You have:

CURRENT_USER
USER_NAME()
SUSER_SNAME()

If you get frustrated building audit triggers, you can look at our OmniAudit
product for doing exactly that:

http://www.krell-software.com/omniaudit


--
Steve Troxell

DHatheway
10/8/2004 9:22:36 AM
The SAMS book is a good resource for learning how to user SQL Server but
isn't a great resource for looking things like this up. You can more
readily get the answer you need by getting familiar with SQL Server Books On
Line (BOL). Install or reinstall the tools on your desktop machine and load
all the documentation to your hard disk for best performance (and
availability). There is an option to use BOL off the CD but it will be
slower and whenever you want to use BOL, you'll have to remove your music CD
(and I just hate doing that).

Then, this problem will depend on how the users are connecting to the
database. If you're using an application UserID (where the credentials for
the connection are actually buried in the app and are the same for each
user), then you won't get much information about the individual who's
actually using the application. However, if you're using trusted
connections or you've set up SQL Server LogonIDs for each user, you could
see if:

select suser_sname()

- or -

select system_user

gives you the result you want. I think there are also some global variables
that you could examine to see if they'll help you.

Really, time spent in BOL getting familiar with how to find things is not
time wasted. Remember that whenever you hit a topic, you should check the
"See Also" list at the bottom of each page.

[quoted text, click to view]

William F. O'Neill
10/8/2004 11:51:26 AM
Thanks for all the help, and suggestions.

[quoted text, click to view]

AddThis Social Bookmark Button