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, 2011 at 06:14 AM in Default

avatar image

2.2k 18 24 31

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

1 answer: 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, 2011 at 06:36 AM

avatar image

Kev Riley ♦♦
66.8k 48 65 81

Ahhh, you beat me with 19seconds. :)

Oct 21, 2011 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, 2011 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, 2011 at 07:00 AM Kev Riley ♦♦

Thanks! That's fine to me!

Oct 21, 2011 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, 2011 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



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Oct 21, 2011 at 06:14 AM

Seen: 5036 times

Last Updated: Oct 21, 2011 at 06:14 AM

Copyright 2018 Redgate Software. Privacy Policy