ALTER USER (Transact-SQL)

**APPLIES TO:** ![yes](media/yes.png)SQL Server (starting with 2008) ![yes](media/yes.png)Azure SQL Database ![yes](media/yes.png)Azure SQL Data Warehouse ![yes](media/yes.png)Parallel Data Warehouse

Renames a database user or changes its default schema.

Topic link icon Transact-SQL Syntax Conventions

Syntax

-- Syntax for SQL Server  
  
ALTER USER userName    
     WITH <set_item> [ ,...n ]  
[;]  
  
<set_item> ::=   
      NAME = newUserName   
    | DEFAULT_SCHEMA = { schemaName | NULL }  
    | LOGIN = loginName  
    | PASSWORD = 'password' [ OLD_PASSWORD = 'oldpassword' ]  
    | DEFAULT_LANGUAGE = { NONE | <lcid> | <language name> | <language alias> }  
    | ALLOW_ENCRYPTED_VALUE_MODIFICATIONS = [ ON | OFF ]  
-- Syntax for Azure SQL Database  
  
ALTER USER userName    
     WITH <set_item> [ ,...n ]  
  
<set_item> ::=   
      NAME = newUserName   
    | DEFAULT_SCHEMA = schemaName  
    | LOGIN = loginName  
    | ALLOW_ENCRYPTED_VALUE_MODIFICATIONS = [ ON | OFF ]   
[;]  
  
-- Azure SQL Database Update Syntax  
ALTER USER userName    
     WITH <set_item> [ ,...n ]  
[;]  
  
<set_item> ::=   
      NAME = newUserName   
    | DEFAULT_SCHEMA = { schemaName | NULL }  
    | LOGIN = loginName  
    | PASSWORD = 'password' [ OLD_PASSWORD = 'oldpassword' ]  
    | ALLOW_ENCRYPTED_VALUE_MODIFICATIONS = [ ON | OFF ]   
  
-- SQL Database syntax when connected to a federation member  
ALTER USER userName  
     WITH <set_item> [ ,… n ]   
[;]  
  
<set_item> ::=   
     NAME = newUserName  
-- Syntax for Azure SQL Data Warehouse and Parallel Data Warehouse  
  
ALTER USER userName    
     WITH <set_item> [ ,...n ]  
  
<set_item> ::=   
     NAME =newUserName   
     | LOGIN =loginName  
     | DEFAULT_SCHEMA = schema_name  
[;]  

Arguments

userName
Specifies the name by which the user is identified inside this database.

LOGIN =loginName
Re-maps a user to another login by changing the user’s Security Identifier (SID) to match the login’s SID.

If the ALTER USER statement is the only statement in a SQL batch, Windows Azure SQL Database supports the WITH LOGIN clause. If the ALTER USER statement is not the only statement in a SQL batch or is executed in dynamic SQL, the WITH LOGIN clause is not supported.

NAME =newUserName
Specifies the new name for this user. newUserName must not already occur in the current database.

DEFAULT_SCHEMA = { schemaName | NULL }
Specifies the first schema that will be searched by the server when it resolves the names of objects for this user. Setting the default schema to NULL removes a default schema from a Windows group. The NULL option cannot be used with a Windows user.

PASSWORD =password
Applies to: SQL Server 2012 (11.x) through SQL Server 2012 (11.x) SQL Server 2017 SQL Server 2012 (11.x) SQL Server 2017 SQL Database

Specifies the password for the user that is being changed. Passwords are case-sensitive.

[!NOTE]
This option is available only for contained users. See Contained Databases and sp_migrate_user_to_contained (Transact-SQL) for more information.

OLD_PASSWORD =‘oldpassword’
Applies to: SQL Server 2012 (11.x) through SQL Server 2012 (11.x) SQL Server 2017 SQL Server 2012 (11.x) SQL Server 2017 SQL Database

The current user password that will be replaced by ‘password’. Passwords are case-sensitive. OLD_PASSWORD is required to change a password, unless you have ALTER ANY USER permission. Requiring OLD_PASSWORD prevents users with IMPERSONATION permission from changing the password.

[!NOTE]
This option is available only for contained users.

DEFAULT_LANGUAGE ={ NONE | <lcid> | <language name> | <language alias> }
Applies to: SQL Server 2012 (11.x) through SQL Server 2012 (11.x) SQL Server 2017

Specifies a default language to be assigned to the user. If this option is set to NONE, the default language is set to the current default language of the database. If the default language of the database is later changed, the default language of the user will remain unchanged. DEFAULT_LANGUAGE can be the local ID (lcid), the name of the language, or the language alias.

[!NOTE]
This option may only be specified in a contained database and only for contained users.

ALLOW_ENCRYPTED_VALUE_MODIFICATIONS = [ ON | OFF ] ]
Applies to: SQL Server 2016 (13.x) through SQL Server 2016 (13.x) SQL Server 2017 SQL Server 2016 (13.x) SQL Server 2017 SQL Database

Suppresses cryptographic metadata checks on the server in bulk copy operations. This enables the user to bulk copy encrypted data between tables or databases, without decrypting the data. The default is OFF.

[!WARNING]
Improper use of this option can lead to data corruption. For more information, see Migrate Sensitive Data Protected by Always Encrypted.

Remarks

The default schema will be the first schema that will be searched by the server when it resolves the names of objects for this database user. Unless otherwise specified, the default schema will be the owner of objects created by this database user.

If the user has a default schema, that default schema will used. If the user does not have a default schema, but the user is a member of a group that has a default schema, the default schema of the group will be used. If the user does not have a default schema, and is a member of more than one group, the default schema for the user will be that of the Windows group with the lowest principal_id and an explicitly set default schema. If no default schema can be determined for a user, the dbo schema will be used.

DEFAULT_SCHEMA can be set to a schema that does not currently occur in the database. Therefore, you can assign a DEFAULT_SCHEMA to a user before that schema is created.

DEFAULT_SCHEMA cannot be specified for a user who is mapped to a certificate, or an asymmetric key.

[!IMPORTANT]
The value of DEFAULT_SCHEMA is ignored if the user is a member of the sysadmin fixed server role. All members of the sysadmin fixed server role have a default schema of dbo.

You can change the name of a user who is mapped to a Windows login or group only when the SID of the new user name matches the SID that is recorded in the database. This check helps prevent spoofing of Windows logins in the database.

The WITH LOGIN clause enables the remapping of a user to a different login. Users without a login, users mapped to a certificate, or users mapped to an asymmetric key cannot be re-mapped with this clause. Only SQL users and Windows users (or groups) can be remapped. The WITH LOGIN clause cannot be used to change the type of user, such as changing a Windows account to a SQL Server login.

The name of the user will be automatically renamed to the login name if the following conditions are true.

Otherwise, the user will not be renamed unless the caller additionally invokes the NAME clause.

The name of a user mapped to a SQL Server login, a certificate, or an asymmetric key cannot contain the backslash character (\).

[!CAUTION]
Beginning with SQL Server 2005, the behavior of schemas changed. As a result, code that assumes that schemas are equivalent to database users may no longer return correct results. Old catalog views, including sysobjects, should not be used in a database in which any of the following DDL statements have ever been used: CREATE SCHEMA, ALTER SCHEMA, DROP SCHEMA, CREATE USER, ALTER USER, DROP USER, CREATE ROLE, ALTER ROLE, DROP ROLE, CREATE APPROLE, ALTER APPROLE, DROP APPROLE, ALTER AUTHORIZATION. In such databases you must instead use the new catalog views. The new catalog views take into account the separation of principals and schemas that was introduced in SQL Server 2005. For more information about catalog views, see [Catalog Views (Transact-SQL)](../relational-databases/system-catalog-views/catalog-views-transact-sql.md).

Security

[!NOTE]
A user who has ALTER ANY USER permission can change the default schema of any user. A user who has an altered schema might unknowingly select data from the wrong table or execute code from the wrong schema.

Permissions

To change the name of a user requires the ALTER ANY USER permission.

To change the target login of a user requires the CONTROL permission on the database.

To change the user name of a user having CONTROL permission on the database requires the CONTROL permission on the database.

To change the default schema or language requires ALTER permission on the user. Users can change their own default schema or language.

Examples

All examples are executed in a user database.

A. Changing the name of a database user

The following example changes the name of the database user Mary5 to Mary51.

ALTER USER Mary5 WITH NAME = Mary51;  
GO  

B. Changing the default schema of a user

The following example changes the default schema of the user Mary51 to Purchasing.

ALTER USER Mary51 WITH DEFAULT_SCHEMA = Purchasing;  
GO  

C. Changing several options at once

The following example changes several options for a contained database user in one statement.

Applies to: SQL Server 2012 (11.x) through SQL Server 2012 (11.x) SQL Server 2017

ALTER USER Philip   
WITH  NAME = Philipe   
    , DEFAULT_SCHEMA = Development   
    , PASSWORD = 'W1r77TT98%ab@#’ OLD_PASSWORD = 'New Devel0per'   
    , DEFAULT_LANGUAGE  = French ;  
GO  

See Also

CREATE USER (Transact-SQL)
DROP USER (Transact-SQL)
Contained Databases
EVENTDATA (Transact-SQL)
sp_migrate_user_to_contained (Transact-SQL)