简体   繁体   中英

Tracking Windows User ID on a Database

Here's the problem in short. I have a desktop application that connects to a SQL Server database. I have an audit table that tracks any changes to the database at the db level. One of the requirements is to track the User Id based on windows login. Is there a way to do this via triggers on the database?

I tried to use USER_NAME(USER_ID()) but all I get is "dbo"

Or should I create a separate stored proc to pass in the user ID from the desktop application to the database? I don't want to go this route because if anyone changes the db in sql server then I won't be able to track it.

  • ORIGINAL_LOGIN returns the name of the login that connected to the instance of SQL Server
  • SUSER_SNAME() returns the current login name

The two return the same login name, except if EXECUTE AS is used.

Use ORIGINAL_LOGIN() thats whats used for auditing as it returns the identity of the user that connected regardless whether the context is impersonated or not.

This have a similar response to what I had answered a while ago here -> SQL Server - current user name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM