SUSER_SNAME (Transact-SQL)SUSER_SNAME (Transact-SQL)

适用对象:是SQL Server 是Azure SQL 数据库 是Azure Synapse Analytics (SQL DW) 是并行数据仓库 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure Synapse Analytics (SQL DW) yesParallel Data Warehouse

返回与安全标识号 (SID) 关联的登录名。Returns the login name associated with a security identification number (SID).

“主题链接”图标 Transact-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

SUSER_SNAME ( [ server_user_sid ] )   

参数Arguments

server_user_sid server_user_sid
适用于SQL Server 2008SQL Server 2008 及更高版本Applies to: SQL Server 2008SQL Server 2008 and later

可选的登录安全标识号。Is the optional login security identification number. server_user_sid 为 varbinary(85) 。server_user_sid is varbinary(85). server_user_sid 可以是任何 SQL ServerSQL Server 登录或 MicrosoftMicrosoft Windows 用户或组的安全标识号 。server_user_sid can be the security identification number of any SQL ServerSQL Server login or MicrosoftMicrosoft Windows user or group. 如果未指定 server_user_sid,则返回有关当前用户的信息 。If server_user_sid is not specified, information about the current user is returned. 如果此参数包含词 NULL,将返回 NULL。If the parameter contains the word NULL will return NULL.

返回类型Return Types

nvarchar(128)nvarchar(128)

RemarksRemarks

SUSER_SNAME 在 ALTER TABLE 或 CREATE TABLE 中可用作 DEFAULT 约束。SUSER_SNAME can be used as a DEFAULT constraint in either ALTER TABLE or CREATE TABLE. SUSER_SNAME 可以在选择列表、WHERE 子句和任何允许使用表达式的地方使用。SUSER_SNAME can be used in a select list, in a WHERE clause, and anywhere an expression is allowed. SUSER_SNAME 必须始终后跟括号,即使在未指定参数的情况下也是如此。SUSER_SNAME must always be followed by parentheses, even if no parameter is specified.

在无参数的情况下调用时,SUSER_SNAME 返回当前安全上下文的名称。When called without an argument, SUSER_SNAME returns the name of the current security context. 当通过使用 EXECUTE AS 切换上下文的批中无参数调用 SUSER_SNAME 时,将返回模拟上下文的名称。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. 从模拟上下文中调用时,ORIGINAL_LOGIN 将返回原始上下文的名称。When called from an impersonated context, ORIGINAL_LOGIN returns the name of the original context.

Azure SQL 数据库Azure SQL Database 备注Remarks

SUSER_NAME 始终返回当前安全上下文的登录名。SUSER_NAME always return the login name for the current security context.

SUSER_SNAME 语句不支持通过 EXECUTE AS 使用模拟安全上下文执行。The SUSER_SNAME statement does not support execution using an impersonated security context through EXECUTE AS.

示例Examples

A.A. 使用 SUSER_SNAMEUsing SUSER_SNAME

下面的示例返回当前安全上下文的登录名。The following example returns the login name for the current security context.

SELECT SUSER_SNAME();  
GO  

B.B. 使用带 Windows 用户安全 ID 的 SUSER_SNAMEUsing SUSER_SNAME with a Windows user security ID

以下示例返回与 Windows 安全标识号关联的登录名。The following example returns the login name associated with a Windows security identification number.

适用于SQL Server 2008SQL Server 2008 及更高版本Applies to: SQL Server 2008SQL Server 2008 and later

SELECT SUSER_SNAME(0x010500000000000515000000a065cf7e784b9b5fe77c87705a2e0000);  
GO  

C.C. 将 SUSER_SNAME 用作 DEFAULT 约束Using SUSER_SNAME as a DEFAULT constraint

下面的示例在 SUSER_SNAME 语句中使用 DEFAULT 作为 CREATE TABLE 约束。The following example uses SUSER_SNAME as a DEFAULT constraint in a CREATE TABLE statement.

USE AdventureWorks2012;  
GO  
CREATE TABLE sname_example  
(  
login_sname sysname DEFAULT SUSER_SNAME(),  
employee_id uniqueidentifier DEFAULT NEWID(),  
login_date  datetime DEFAULT GETDATE()  
);   
GO  
INSERT sname_example DEFAULT VALUES;  
GO  

D.D. 与 EXECUTE AS 一起调用 SUSER_SNAMECalling SUSER_SNAME in combination with EXECUTE AS

该示例显示了从模拟上下文调用时的 SUSER_SNAME 的行为。This example shows the behavior of SUSER_SNAME when called from an impersonated context.

适用于SQL Server 2008SQL Server 2008 及更高版本Applies to: SQL Server 2008SQL Server 2008 and later

SELECT SUSER_SNAME();  
GO  
EXECUTE AS LOGIN = 'WanidaBenShoof';  
SELECT SUSER_SNAME();  
REVERT;  
GO  
SELECT SUSER_SNAME();  
GO  
  

下面是结果。Here is the result.

sa  
WanidaBenShoof  
sa

示例:Azure Synapse Analytics (SQL DW)Azure Synapse Analytics (SQL DW)并行数据仓库Parallel Data WarehouseExamples: Azure Synapse Analytics (SQL DW)Azure Synapse Analytics (SQL DW) and 并行数据仓库Parallel Data Warehouse

E.E. 使用 SUSER_SNAMEUsing SUSER_SNAME

以下示例返回值为 0x01 的安全标识号的登录名。The following example returns the login name for the security identification number with a value of 0x01.

SELECT SUSER_SNAME(0x01);  
GO  

F.F. 返回当前登录名Returning the Current Login

以下示例返回当前登录的登录名称。The following example returns the login name of the current login.

SELECT SUSER_SNAME() AS CurrentLogin;  
GO  

另请参阅See Also

SUSER_SID (Transact-SQL) SUSER_SID (Transact-SQL)
主体(数据库引擎) Principals (Database Engine)
sys.server_principals (Transact-SQL) sys.server_principals (Transact-SQL)
EXECUTE AS (Transact-SQL)EXECUTE AS (Transact-SQL)