Difference between ORIGINAL_LOGIN() and SUSER_SNAME() | SansSQL

Wednesday, February 26, 2014

Difference between ORIGINAL_LOGIN() and SUSER_SNAME()

The function ORIGINAL_LOGIN() , returns the name of the login that was initially connected to the SQL Server instance. We can use this function to return the identity of the original login in sessions in which there are many explicit or implicit context/connection switches.

The function SUSER_SNAME() , returns the name of the login that is currently connected to the SQL Server instance.

Here is small demo on working of ORIGINAL_LOGIN() and SUSER_SNAME()

-- Initial Details
SELECT ORIGINAL_LOGIN() AS [OriginalLoginSession], SUSER_SNAME() AS [CurrentLoginSession]
GO
-- Execute as another Login
EXECUTE AS LOGIN = 'Test'
GO
SELECT ORIGINAL_LOGIN() AS [OriginalLoginSession], SUSER_SNAME() AS [CurrentLoginSession]
GO
-- Revert the Execute as 
REVERT
GO
-- Later Details
SELECT ORIGINAL_LOGIN() AS [OriginalLoginSession], SUSER_SNAME() AS [CurrentLoginSession]
GO

No comments:

Post a Comment