Syntax

SUSER_SNAME ( [ server_user_sid ] )

Arguments

server_user_sidApplies to: SQL Server 2008 through SQL Server 2017

Is the optional login security identification number. server_user_sid is varbinary(85). server_user_sid can be the security identification number of any SQL Server login or Microsoft Windows user or group. If server_user_sid is not specified, information about the current user is returned. If the parameter contains the word NULL will return NULL.

Return Types

nvarchar(128)

Remarks

SUSER_SNAME can be used as a DEFAULT constraint in either ALTER TABLE or CREATE TABLE. SUSER_SNAME can be used in a select list, in a WHERE clause, and anywhere an expression is allowed. SUSER_SNAME must always be followed by parentheses, even if no parameter is specified.

When called without an argument, SUSER_SNAME returns the name of the current security context. When called without an argument within a batch that has switched context by using EXECUTE AS, SUSER_SNAME returns the name of the impersonated context. When called from an impersonated context, ORIGINAL_LOGIN returns the name of the original context.

Azure SQL Database Remarks

SUSER_NAME always return the login name for the current security context.

The SUSER_SNAME statement does not support execution using an impersonated security context through EXECUTE AS.

Examples

A. Using SUSER_SNAME

The following example returns the login name for the current security context.

SELECT SUSER_SNAME();
GO

B. Using SUSER_SNAME with a Windows user security ID

The following example returns the login name associated with a Windows security identification number.