sql-servert-sql

What is the difference between SYSTEM_USER and USER in SQL server?


I am very new to databases and confused between the keywords SYSTEM_USER and USER in MSSQL. The definitions say that they return the username in the current context. but the instance I have running returns 'dbo' for USER and 'sa' for system user.

Can somebody highlight the exact difference between the two?


Solution

  • Examples:

    SELECT SYSTEM_USER      --> myDomain\user.name
    ----------------------------------------------
    SELECT USER             --> dbo
    SELECT CURRENT_USER     --> dbo 
    SELECT USER_NAME()      --> dbo
    SELECT SESSION_USER     --> dbo
    ----------------------------------------------
    

    Note: USER_NAME([user_id]) can additionally take an int-user-id, default arg is 1, i.e.: USER_NAME(1) would be same as USER_NAME().