How to Gather User ID Values in SQL Server: Just use this one helpful function.

SQL Server User ID featured image
Reading Time: < 1 minute

There may be times where it might be helpful to gather the ID of the person or application that is interacting with the data or objects in your database.

Maybe you are trying to audit changes to your data, or maybe you want to get a better idea of how the data is being queried.

The SUSER_SNAME() system function

The SUSER_SNAME() system function quite simply returns the login name for the current security context.

We can call it very simply:

SELECT SUSER_SNAME() AS TheUser

Understand that if we have the Security Identification Number (SID) of a user, we can pass that value as an argument to the function, and it will return to us the login name of the person associated with that ID. Here’s an example:

SELECT SUSER_SNAME(0x7535000777000a067df7ebs4c909abe2020) AS WhoIsThis

(That ID is completely bogus, but in real life, a person’s security ID would look similar to that)

Also understand that if you pass a security ID that doesn’t exist, NULL will be returned.

Next Steps:

Leave a comment if you found this tutorial helpful!

Here’s a link to the official Microsoft article that talks about SUSER_SNAME()



Thank you very much for reading!

Make sure you subscribe to my newsletter to receive special offers and notifications anytime a new tutorial is released!

If you have any questions, or if you are struggling with a different topic related to SQL Server, I’d be happy to discuss it. Leave a comment or visit my contact page and send me an email!

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *