Home > Articles > Data > SQL Server

  • Print
  • + Share This
Like this article? We recommend

USER- and HOST-Related Functions

The CURRENT_USER, SYSTEM_USER, USER_NAME(), and SESSION_USER functions return information about the current user of the system. The output of these functions is useful for enforcing security—you can allow reading or modifying certain data only to authorized users. These functions are also handy when you want to create an audit trail of data changes: Every time someone modifies the data, you can store the user name in the updated_by column, or perhaps even insert a row in the audit tail table.

CURRENT_USER, SESSION_USER and USER_NAME() are equivalent—all three return the current database user name. Notice, however, that the USER_NAME() function accepts a user_id as the parameter, defaulting to the current user if no parameter is passed. All these functions can also be successfully used for auditing purposes. USER_NAME can also be used to look up a database user name by its identifier.

SYSTEM_USER returns the SQL Server or Windows login name, depending on the authentication method used.

HOST_ID() and HOST_NAME() return workstation identifier and name, respectively. They also can be used to troubleshoot problems specific to a particular computer.

  • + Share This
  • 🔖 Save To Your Account