SQL Server Execute Impersonation

JBone picture JBone · Apr 6, 2012 · Viewed 9.4k times · Source

What is the diffrence between...

execute as user = 'testuser'

AND

execute as login = 'testuser'

I am executing a cross database procedure under these logins and it works with the exececute as login but not the execute as user. It is saying the server principal "testuser" is nt able to access the database "xxx" under the securty context.

When i SELECT SYSTEM_USER after both commands I see that it is set to 'testuser'

Answer

goric picture goric · Apr 6, 2012

execute as login provides impersonation to the entire server, since logins are on a server level. Since users are defined per database, execute as user impersonation applies only to a specific database, which is why you see the error when you cross databases.