SQL Server - current user name

Sql ServerTsql

Sql Server Problem Overview


Which one should I use to record update made by users?

  1. SYSTEM_USER, or
  2. ORIGINAL_LOGIN(), or
  3. SUSER_SNAME()

Sql Server Solutions


Solution 1 - Sql Server

SYSTEM_USER returns the current executing context, so this can return an impersonated context

ORIGINAL_LOGIN() returns the identity of the user that initially connected to the instance, so regardless whether the context is impersonated or not it will yield the original user that logged in, good for auditing.

SUSER_SNAME() this is used if you want to get the username by SID so SUSER_SNAME can be invoked with a parameter like such SUSER_SNAME([server_user_sid]) but the SID is optional if you don’t pass that parameter the current user is returned.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSreedharView Question on Stackoverflow
Solution 1 - Sql ServerRaymundView Answer on Stackoverflow