x
login about faq Site discussion (meta-askssc)

SUSER_SNAME() vs. SUSER_NAME(). Which to use?

Whats the difference between SUSER_SNAME() and SUSER_NAME()? Which one should I use?

more ▼

asked Oct 21 '11 at 06:14 AM in Default

eghetto gravatar image

eghetto
1.5k 6 13 17

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

The difference is in the arguments that those functions take.

SUSER_SNAME(), takes an optional varbinary(85) argument, which is the security identification number of a SQL Server login / windows login / windows group. You can get your SID by executing select SUSER_SID()

SUSER_NAME(), takes an optional integer argument, which is the login id of a SQL server login / windows user / windows group. You can get your login id by executing select SUSER_ID()

Without arguments, they both return the current user.

Which one should you use - depends on what are you trying to do?

more ▼

answered Oct 21 '11 at 06:36 AM

Kev Riley gravatar image

Kev Riley ♦♦
46.1k 39 43 69

Ahhh, you beat me with 19seconds. :)

Oct 21 '11 at 06:37 AM Håkan Winther

Well, I'm trying to find out who is executing my stored procedure! Is there a situation where SUSER_SNAME() and SUSER_NAME() return different results? Or can I mix them around im my code?

Oct 21 '11 at 06:44 AM eghetto

I'm not aware of a situation where they can return different results, when called without arguments, even in impersonation contexts.

I would just choose one and then stick with that one!

Oct 21 '11 at 07:00 AM Kev Riley ♦♦

Thanks! That's fine to me!

Oct 21 '11 at 07:09 AM eghetto

@Håkan - you shouldn't have deleted yours - you made a good point about a change in SQL 7 - provides a bit of background :)

Oct 21 '11 at 07:12 AM Kev Riley ♦♦
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x49
x9

asked: Oct 21 '11 at 06:14 AM

Seen: 1734 times

Last Updated: Oct 21 '11 at 06:14 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.