sp_grantdbaccess (Transact-SQL)sp_grantdbaccess (Transact-SQL)

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

将数据库用户添加到当前数据库。Adds a database user to the current database.


此功能处于维护模式并且可能会在 Microsoft SQL Server 将来的版本中被删除。This feature is in maintenance mode and may be removed in a future version of Microsoft SQL Server. 请避免在新的开发工作中使用该功能,并着手修改当前还在使用该功能的应用程序。Avoid using this feature in new development work, and plan to modify applications that currently use this feature.请改用CREATE USERUse CREATE USER instead.

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


sp_grantdbaccess [ @loginame = ] 'login'  
    [ , [ @name_in_db = ] 'name_in_db' [ OUTPUT ] ]  


[ @loginame = ] 'login_ '要映射到新数据库用户的 Windows 组、Windows SQL ServerSQL Server登录名或登录名。[ @loginame = ] 'login_ ' Is the name of the Windows group, Windows login or SQL ServerSQL Server login to be mapped to the new database user. Windows 组和 windows 登录名的名称必须以\登录名的形式使用 windows 域名进行限定。例如, LONDON\JoebNames of Windows groups and Windows logins must be qualified with a Windows domain name in the form Domain\login; for example, LONDON\Joeb. 登录名不能已映射到数据库中的用户。The login cannot already be mapped to a user in the database. loginsysname,无默认值。login is a sysname, with no default.

[ @name_in_db = ] 'name_in_db' [ OUTPUT]新数据库用户的名称。[ @name_in_db = ] 'name_in_db' [ OUTPUT] Is the name for the new database user. name_in_db是数据类型为sysname的输出变量,默认值为 NULL。name_in_db is an OUTPUT variable with a data type of sysname, and a default of NULL. 如果未指定,则使用登录名If not specified, login is used. 如果指定为值为 NULL 的输出变量, ** @则 name_in_db*设置为login*。If specified as an OUTPUT variable with a value of NULL, @name_in_db is set to login. 当前数据库中不能存在name_in_dbname_in_db must not already exist in the current database.

返回代码值Return Code Values

0(成功)或 1(失败)0 (success) or 1 (failure)


sp_grantdbaccess调用 CREATE USER,后者支持其他选项。sp_grantdbaccess calls CREATE USER, which supports additional options. 有关创建数据库用户的信息,请参阅CREATE USER (transact-sql)For information about creating database users, see CREATE USER (Transact-SQL). 若要从数据库中删除数据库用户,请使用DROP userTo remove a database user from a database, use DROP USER.

不能在用户定义的事务中执行sp_grantdbaccesssp_grantdbaccess cannot be executed within a user-defined transaction.


需要db_owner固定数据库角色的成员身份或db_accessadmin固定数据库角色的成员身份。Requires membership in the db_owner fixed database role or the db_accessadmin fixed database role.


下面的示例使用CREATE USER将 Windows 登录名Edmonds\LolanSo的数据库用户添加到当前数据库。The following example uses CREATE USER to add a database user for the Windows login Edmonds\LolanSo to the current database. 新用户名为 LolanThe new user is named Lolan. 这是创建数据库用户的首选方法。This is the preferred method for creating a database user.

CREATE USER Lolan FOR LOGIN [Edmonds\LolanSo];  

另请参阅See Also

安全存储过程 (Transact-sql) Security Stored Procedures (Transact-SQL)
CREATE USER (Transact-sql) CREATE USER (Transact-SQL)
DROP USER (Transact-sql) DROP USER (Transact-SQL)
系统存储过程 (Transact-SQL)System Stored Procedures (Transact-SQL)