Question

Msg 15199, Level 16, State 1, Line 29 T he current security context cannot be reverted.

Please switch to the original database where Execute As was called and try it again.

execute as login='juno'
REVERT


DECLARE @User VARCHAR(20)
SELECT @USER = SUBSTRING(SUSER_SNAME(), CHARINDEX('\', SUSER_SNAME()) + 1, LEN(SUSER_SNAME()))
SELECT  [THE_SERVER]= @@SERVERNAME
        ,[DB_NAME] =DB_NAME()
        ,[@USER]=@USER 
        ,[SUSER_SNAME()]=SUSER_SNAME()
        ,[SYSTEM_USER]=SYSTEM_USER
        ,[USER_NAME()]=USER_NAME() 
        ,[CURRENT_USER]=CURRENT_USER
        ,[ORIGINAL_LOGIN()]=ORIGINAL_LOGIN()
        ,[USER]=USER
        ,[SESSION_USER]=SESSION_USER

enter image description here

What if I can't remember the original database I was when I executed the execute as login?

Is there a way to find out where I was?

Was it helpful?

Solution

There is no way to find it out, but you can try all the databases accessible by that login, i.e. under impersonated login execute the following code:

select 'use ' + name + '; revert;'
from sys.databases
where HAS_DBACCESS(name) = 1;

This way you find all the databases from which you could impersonate this login. Then you copy the output to query window and try it row by row until success.

But the easiest way to return yourself is just to change connection (right click in query window)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top