SETUSER (Transact-SQL)

Allows a member of the sysadmin fixed server role or db_owner fixed database role to impersonate another user.

Important

SETUSER is included for backward compatibility only. SETUSER may not be supported in a future release of SQL Server. We recommend that you use EXECUTE AS instead.

Topic link iconTransact-SQL Syntax Conventions

Syntax

SETUSER [ 'username' [ WITH NORESET ] ] 

Arguments

  • 'username'
    Is the name of a SQL Server or Windows user in the current database that is impersonated. When username is not specified, the original identity of the system administrator or database owner impersonating the user is reset.

  • WITH NORESET
    Specifies that subsequent SETUSER statements (with no specified username) should not reset the user identity to system administrator or database owner.

Remarks

SETUSER can be used by a member of the sysadmin fixed server role or the db_owner fixed database role to adopt the identity of another user to test the permissions of the other user.

Only use SETUSER with SQL Server users. SETUSER is not supported with Windows users. When SETUSER has been used to assume the identity of another user, any objects that the impersonating user creates are owned by the user being impersonated. For example, if the database owner assumes the identity of user Margaret and creates a table called orders, the orders table is owned by Margaret, not the system administrator.

SETUSER remains in effect until another SETUSER statement is issued or until the current database is changed with the USE statement.

Note

If SETUSER WITH NORESET is used, the database owner or system administrator must log off and then log on again to reestablish his or her own rights.

Permissions

Requires membership in the sysadmin fixed server role or db_owner fixed database role.

Examples

The following example shows how the database owner can adopt the identity of another user. User mary has created a table called computer_types. By using SETUSER, the database owner impersonates mary to grant user joe access to the computer_types table, and then resets his or her own identity.

SETUSER 'mary'
GO
GRANT SELECT ON computer_types TO joe
GO
SETUSER