This is what I normally use to find out what user is being used:
it doesn't though, return the current Windows group via which the user is being granted access to the database.
-- get the current login name
-- marcelo miorelli
-- 26-11-2013
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
and to test the above, or sometimes to test some permission or procedure I do something similar to the example below:
-- to check how a specific procedure will run under a different account
EXECUTE AS LOGIN='mycompany\Celcatsupport'
-- checking who I am running the commands as (impersonating)
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
--reverting back to my original login
REVERT
the following script does not give you which group was used for the current login
but it has been very useful to me in many occasions
--the following query will give you
--all the available windows group that
--have login in the current server
--and @NTLogin belongs to
declare @NTLogin nvarchar(128)
select @NTLogin = 'mycompany\myuser'
DECLARE @UserList TABLE (
[Account Name] nvarchar(128)collate Latin1_General_CI_AS,
[Type] nvarchar(128) collate Latin1_General_CI_AS,
[Privilege] nvarchar(128) collate Latin1_General_CI_AS,
[Mapped Login Name] nvarchar(128)collate Latin1_General_CI_AS,
[Permission Path] nvarchar(128) )
INSERT INTO @UserList EXEC master.dbo.xp_logininfo @NTLogin, 'all' --insert group information
IF EXISTS (SELECT * FROM @UserList WHERE [Type] = 'group') --only if it's a group
INSERT INTO @UserList EXEC master.dbo.xp_logininfo @NTLogin, 'members' --insert member information
SELECT [Server Name] = @@SERVERNAME,
[Account Name],
[Type],
[Privilege],
[Mapped Login Name],
[Permission Path]
FROM @UserList