ALTER ROLE (Transact-SQL)
Applies to: SQL Server (all supported versions)
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform System (PDW)
Adds or removes members to or from a database role, or changes the name of a user-defined database role.
Note
To alter roles adding or dropping members in Azure Synapse Analytics or Analytics Platform System (PDW), use sp_addrolemember (Transact-SQL) and sp_droprolemember (Transact-SQL).
Transact-SQL Syntax Conventions
Syntax
-- Syntax for SQL Server (starting with 2012) and Azure SQL Database
ALTER ROLE role_name
{
ADD MEMBER database_principal
| DROP MEMBER database_principal
| WITH NAME = new_name
}
[;]
-- Syntax for SQL Server 2008, Azure Synapse Analytics and Parallel Data Warehouse
-- Change the name of a user-defined database role
ALTER ROLE role_name
WITH NAME = new_name
[;]
Note
To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
Arguments
role_name
APPLIES TO: SQL Server (starting with 2008), Azure SQL Database
Specifies the database role to change.
ADD MEMBER database_principal
APPLIES TO: SQL Server (starting with 2012), Azure SQL Database
Specifies to add the database principal to the membership of a database role.
database_principal is a database user or a user-defined database role.
database_principal cannot be a fixed database role or a server principal.
DROP MEMBER database_principal
APPLIES TO: SQL Server (starting with 2012), Azure SQL Database
Specifies to remove a database principal from the membership of a database role.
database_principal is a database user or a user-defined database role.
database_principal cannot be a fixed database role or a server principal.
WITH NAME = new_name
APPLIES TO: SQL Server (starting with 2008), Azure SQL Database
Specifies to change the name of a user-defined database role. The new name must not already exist in the database.
Changing the name of a database role does not change ID number, owner, or permissions of the role.
Permissions
To run this command you need one or more of these permissions or memberships:
- ALTER permission on the role
- ALTER ANY ROLE permission on the database
- Membership in the db_securityadmin fixed database role
Additionally, to change the membership in a fixed database role you need:
- Membership in the db_owner fixed database role
Limitations and restrictions
You cannot change the name of a fixed database role.
Metadata
These system views contain information about database roles and database principals.
Examples
A. Change the name of a database role
APPLIES TO: SQL Server (starting with 2008), SQL Database
The following example changes the name of role buyers
to purchasing
. This example can be executed in the AdventureWorks sample database.
ALTER ROLE buyers WITH NAME = purchasing;
B. Add or remove role members
APPLIES TO: SQL Server (starting with 2012), SQL Database
This example creates a database role named Sales
. It adds a database user named Barry to the membership, and then shows how to remove the member Barry. This example can be executed in the AdventureWorks sample database.
CREATE ROLE Sales;
ALTER ROLE Sales ADD MEMBER Barry;
ALTER ROLE Sales DROP MEMBER Barry;
C. Add a role member to special roles for Azure SQL Database and Azure Synapse Analytics
APPLIES TO: SQL Server (Azure SQL Database and Azure Synapse), SQL Database
This example creates a SQL login in the master database, creates a database user that's related to that server login, and adds the database user as a member of the special role dbmanager
. The example allows the user permissions to create and drop databases on an Azure SQL Database logical server. Run the example on the master database of the Azure SQL Database logical server.
CREATE LOGIN sqllogin_nlastname WITH password='aah3%#om1os';
CREATE USER sqllogin_nlastname FOR LOGIN sqllogin_nlastname
WITH DEFAULT_SCHEMA = master;
ALTER ROLE [dbmanager] add member sqllogin_nlastname;
See Also
CREATE ROLE (Transact-SQL)
Principals (Database Engine)
DROP ROLE (Transact-SQL)
sp_addrolemember (Transact-SQL)
sys.database_role_members (Transact-SQL)
sys.database_principals (Transact-SQL)
Feedback
Submit and view feedback for