sp_addrole (Transact-SQL)sp_addrole (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

在当前数据库中创建新的数据库角色。Creates a new database role in the current database.

重要

sp_addrole包含有关与早期版本的兼容性MicrosoftMicrosoftSQL ServerSQL Server在将来的版本中可能不支持。sp_addrole is included for compatibility with earlier versions of MicrosoftMicrosoftSQL ServerSQL Server and may not be supported in a future release. 使用CREATE ROLE相反。Use CREATE ROLE instead.

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

语法Syntax

  
sp_addrole [ @rolename = ] 'role' [ , [ @ownername = ] 'owner' ]   

参数Arguments

[ @rolename = ] 'role' 为新的数据库角色的名称。[ @rolename = ] 'role' Is the name of the new database role. 角色sysname,无默认值。role is a sysname, with no default. 角色必须是有效的标识符 (ID),并且必须已存在当前数据库中。role must be a valid identifier (ID) and must not already exist in the current database.

[ @ownername = ] 'owner' 为新的数据库角色的所有者。[ @ownername = ] 'owner' Is the owner of the new database role. 所有者sysname,默认值为当前正在执行的用户。owner is a sysname, with a default of the current executing user. 所有者必须是数据库用户或当前数据库中的数据库角色。owner must be a database user or database role in the current database.

返回代码值Return Code Values

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

备注Remarks

SQL ServerSQL Server 数据库角色的名称可以包含 1 到 128 个字符,包括字母、符号和数字。The names of SQL ServerSQL Server database roles can contain from 1 through 128 characters, including letters, symbols, and numbers. 数据库角色的名称不能: 包含反斜杠字符 (\),可以为 NULL,则为空字符串 ( ' )。The names of database roles cannot :contain a backslash character (\), be NULL, or an empty string ('').

添加数据库角色后,使用sp_addrolemember (TRANSACT-SQL) 将主体添加到角色。After you add a database role, use sp_addrolemember (Transact-SQL) to add principals to the role. 当使用 GRANT、DENY 或 REVOKE 语句将权限应用于数据库角色时,数据库角色的成员将继承这些权限,就好像将权限直接应用于其帐户一样。When GRANT, DENY, or REVOKE statements are used to apply permissions to the database role, members of the database role inherit those permissions as if the permissions were applied directly to their accounts.

备注

不能创建新服务器角色。New server roles cannot be created. 只能在数据库级别上创建角色。Roles can only be created at the database level.

sp_addrole不能在用户定义的事务内使用。sp_addrole cannot be used inside a user-defined transaction.

权限Permissions

需要对数据库具有 CREATE ROLE 权限。Requires CREATE ROLE permission on the database. 如果创建架构,则需要对数据库的 CREATE SCHEMA 权限。If creating a schema, requires CREATE SCHEMA on the database. 如果所有者指定为用户或组,则该用户或组需要模拟。If owner is specified as a user or group, requires IMPERSONATE on that user or group. 如果所有者指定作为角色,则需要对该角色或该角色的成员的 ALTER 权限。If owner is specified as a role, requires ALTER permission on that role or on a member of that role. 如果将所有者指定为应用程序角色,则需要对此应用程序角色的 ALTER 权限。If owner is specified as an application role, requires ALTER permission on that application role.

示例Examples

以下示例向当前数据库中添加名为 Managers 的新角色。The following example adds a new role called Managers to the current database.

EXEC sp_addrole 'Managers';  

请参阅See Also

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