x

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

eghetto
2.1k 18 23 30

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

avatar image

Kev Riley ♦♦
63.8k 48 61 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

By RSS:

Answers

Answers and Comments

SQL Server Central

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

Topics:

x68
x16

asked: Oct 21, 2011 at 06:14 AM

Seen: 4324 times

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

Copyright 2016 Redgate Software. Privacy Policy