tags:

views:

21

answers:

2

When I logged in sql management studio with windows authentication and I run

SELECT USER_NAME()

I see the result as dbo.

I would of thought that it would showed my user ....

I more looking at the explaination to as why it returns dbo

+3  A: 

Use this instead

SELECT SUSER_NAME()

USER_NAME: Returns a database user name from a specified identification number.

SUSER_NAME: Returns the login identification name of the user.

Martin Smith
I'm more interested in why it shows dbo. Do you know why it returns dbo?
pdiddy
@pdiddy - What did you expect it to return? This function will return a database user name - i.e. a value from `sysusers`.
Martin Smith
Can I logged in as the database user into Management studio?
pdiddy
@pdiddy - When you login to the server you do so by giving the credentials of a server login. You don't login as a specific database user. These server logins are mapped to 0..n database users. If you are sysadmin you will automatically be mapped to the dbo user in all databases.
Martin Smith
Great explaination. But In Server Roles for my logins, I do not see the sysadmin being checked but public.
pdiddy
Are you an administrator on the machine?
Martin Smith
oh that would explain it. Thanks! so the only way would be to impersonate the database user.
pdiddy
+1  A: 

Try:

SELECT SUSER_SNAME()

Zamboni
already one same answer is there
anishmarokey
@anish - Not quite. I said `SUSER_NAME`. Not sure how that it is different from `SUSER_SNAME`
Martin Smith
mine is suser_sname; they appear interchangable. Martin's answer was first should be considered correct.
Zamboni
I did a quick check in some SQL Server 2000 help and found the following comment: SUSER_NAME always returns NULL when used in Microsoft® SQL Server™ 2000. This system built-in function is included only for backward compatibility. Use SUSER_SNAME instead.
Zamboni
No mention of deprecation in [the SQL Server 2008 docs](http://msdn.microsoft.com/en-us/library/ms187934.aspx). Wonder if they just decided to keep it.
Martin Smith