Get the Current Login ID in SQL Server (T-SQL)

You can use the SUSER_ID() function to return the login identification number of the current user.

You can also use it to return the login ID of another user.

This is similar to returning the current login name, except here we’re returning the ID instead.

Example

Here I return my own login identification number.

SELECT SUSER_ID();

Return:

1

In this case, I was logged in as sa and its login ID is 1.

Get Another User’s ID

To get the login ID of another user, simply provide that user’s login identification name as an argument.

SELECT SUSER_ID('Rick');

Result:

262

Include the Login Name & Workstation

Here’s an example that returns the login name and workstation along with the login ID.

SELECT 
  HOST_NAME() AS HOST_NAME,
  SUSER_ID() AS SUSER_ID,
  SUSER_NAME() AS SUSER_NAME;

Result:

+---------------------+------------+--------------+
 | HOST_NAME           | SUSER_ID   | SUSER_NAME   |
 |---------------------+------------+--------------|
 | Ricks-MacBook-Pro   | 262        | Rick         |
 +---------------------+------------+--------------+ 

In this case, the currently logged in user was Rick.