Returns the security identification number (SID) for the specified login name.
Transact-SQL Syntax Conventions
SUSER_SID ( [ 'login' ] [ , Param2 ] )
’ login ’
Applies to: SQL Server 2008 through SQL Server 2008 SQL Server 2017
Is the login name of the user. login is sysname. login, which is optional, can be a SQL Server login or SQL Server Microsoft Windows user or group. If login is not specified, information about the current security context is returned. If the parameter contains the word NULL will return NULL.
Param2
Applies to: SQL Server 2012 (11.x) through SQL Server 2012 (11.x) SQL Server 2017
Specifies whether the login name is validated. Param2 is of type int and is optional. When Param2 is 0, the login name is not validated. When Param2 is not specified as 0, the Windows login name is verified to be exactly the same as the login name stored in SQL Server
varbinary(85)
SUSER_SID can be used as a DEFAULT constraint in either ALTER TABLE or CREATE TABLE. SUSER_SID can be used in a select list, in a WHERE clause, and anywhere an expression is allowed. SUSER_SID must always be followed by parentheses, even if no parameter is specified.
When called without an argument, SUSER_SID returns the SID of the current security context. When called without an argument within a batch that has switched context by using EXECUTE AS, SUSER_SID returns the SID of the impersonated context. When called from an impersonated context, SUSER_SID(ORIGINAL_LOGIN()) returns the SID of the original context.
When the SQL Server collation and the Windows collation are different, SUSER_SID can fail when SQL Server SQL Server and Windows store the login in a different format. For example, if the Windows computer TestComputer has the login User and SQL Server SQL Server SQL Server stores the login as TESTCOMPUTER, the lookup of the login TestComputermight fail to resolve the login name correctly. To skip this validation of the login name, use Param2. Differing collations is often a cause of SQL Server SQL Server SQL Server SQL Server error 15401:
Windows NT user or group '%s' not found. Check the name again.
The followng example returns the security identification number (SID) for the current security context.
SELECT SUSER_SID();
The following example returns the security identification number for the SQL Server sa
login.
Applies to: SQL Server 2012 (11.x) through SQL Server 2012 (11.x) SQL Server 2017
SELECT SUSER_SID('sa');
GO
The following example returns the security identification number for the Windows user London\Workstation1
.
Applies to: SQL Server 2012 (11.x) through SQL Server 2012 (11.x) SQL Server 2017
SELECT SUSER_SID('London\Workstation1');
GO
The following example uses SUSER_SID
as a DEFAULT
constraint in a CREATE TABLE
statement.
USE AdventureWorks2012;
GO
CREATE TABLE sid_example
(
login_sid varbinary(85) DEFAULT SUSER_SID(),
login_name varchar(30) DEFAULT SYSTEM_USER,
login_dept varchar(10) DEFAULT 'SALES',
login_date datetime DEFAULT GETDATE()
);
GO
INSERT sid_example DEFAULT VALUES;
GO
The following example shows how to use Param2 to obtain the SID from Windows and uses that SID as an input to the SUSER_SNAME
function. The example provides the login in the format in which it is stored in Windows (TestComputer\User
), and returns the login in the format in which it is stored in SQL Server (TESTCOMPUTER\User
).
Applies to: SQL Server 2012 (11.x) through SQL Server 2012 (11.x) SQL Server 2017
SELECT SUSER_SNAME(SUSER_SID('TestComputer\User', 0));
ORIGINAL_LOGIN (Transact-SQL)
CREATE TABLE (Transact-SQL)
binary and varbinary (Transact-SQL)
System Functions (Transact-SQL)