SQL Server CURRENT_USER Function: How to Use It
In this article
The CURRENT_USER function in SQL Server returns the name of the current user in the current database context, in the format database_user or domain\login for contained databases. It requires no arguments and can be used anywhere a scalar expression is allowed. To use it, simply call SELECT CURRENT_USER; in a query window. This guide walks you through the steps, common pitfalls, and troubleshooting so you can rely on it in permissions checks, auditing, and dynamic SQL.
What You'll Need
- Access to a SQL Server instance (any recent version, including Azure SQL Database).
- A client tool such as SQL Server Management Studio (SSMS) or Azure Data Studio.
- Permission to connect and run a simple SELECT statement—no special rights are required.
Step-by-Step
- Connect to your database. Open SSMS or Azure Data Studio, connect to the instance, and select the database you want to test in the Available Databases dropdown or with
USE YourDatabase;. The function returns the user in the current database context, so the selected database matters. - Run a basic query. In a new query window, execute:
SELECT CURRENT_USER AS CurrentUser;
You will see one row with the database user name. If you are connected with Windows authentication, this is oftendboor your domain login; with SQL authentication, it is the login mapped to a database user. - Compare with related functions (optional). Run
SELECT CURRENT_USER, USER_NAME(), SUSER_SNAME(), SYSTEM_USER;to see the differences.CURRENT_USERandUSER_NAME()return the database user;SUSER_SNAME()andSYSTEM_USERreturn the server login. This helps you pick the right one for permission checks. - Use it in a WHERE clause or check. For example, to filter rows by owner:
SELECT * FROM dbo.Orders WHERE Owner = CURRENT_USER;
Or to conditionally return a value:SELECT CASE WHEN CURRENT_USER = 'dbo' THEN 'Admin' ELSE 'User' END AS Role; - Test with EXECUTE AS (optional). To see how the function behaves under impersonation, run:
EXECUTE AS USER = 'someuser';
SELECT CURRENT_USER;
REVERT;
This shows the impersonated user. Always includeREVERT;to return to your original context. - Check the result in a contained database. If your database is contained,
CURRENT_USERreturns the contained user name (without the login). This is useful for portability across instances.
Common Mistakes
- Confusing CURRENT_USER with SYSTEM_USER.
CURRENT_USERgives the database user;SYSTEM_USERgives the server login. Use the right one for your security check. - Assuming it returns the login name. In a contained database, it returns the contained user, which may differ from the login used to connect.
- Using it without specifying the database. If you run it in
masterbut expect a user from another database, you will get the user inmaster. Always set the correct database context. - Forgetting that it is non-deterministic. The value can change if the execution context changes (e.g., via
EXECUTE AS), so avoid using it in indexed views or persisted computed columns. - Expecting it to work in a stored procedure without EXECUTE AS. It returns the caller's database user by default; if you need the procedure owner, use
EXECUTE AS OWNER.
Tips
- Use
CURRENT_USERin row-level security predicates or audit triggers to record who performed an action. - Combine it with
IS_MEMBER()to check role membership:SELECT IS_MEMBER('db_owner') AS IsOwner, CURRENT_USER; - In dynamic SQL,
CURRENT_USERreflects the execution context at the time the dynamic batch runs, which is usually what you want. - For cross-database queries, remember that
CURRENT_USERis evaluated in the context of the current database, not the remote one. - Test your logic with different logins to ensure permissions behave as expected.
Troubleshooting
If CURRENT_USER returns an unexpected value, check the following:
- Verify the database context. Run
SELECT DB_NAME();to confirm you are in the intended database. Switch withUSEif needed. - Check for impersonation. If you used
EXECUTE ASearlier and forgot toREVERT, the session may still be impersonating. RunSELECT ORIGINAL_LOGIN();to see the original login, andREVERT;to return. - Confirm the login mapping. In the target database, run
SELECT * FROM sys.database_principals WHERE name = CURRENT_USER;to see the user details. If the user does not exist, you may be connected asguestordbo. - Check for contained databases. If the database is contained, the user may be a contained user without a login. That is expected behavior.
- Permissions issue?
CURRENT_USERitself requires no permissions, but if you cannot run a query, ensure you haveCONNECTpermission on the database.
Frequently Asked Questions
What is the difference between CURRENT_USER and USER_NAME()?
They are functionally equivalent; CURRENT_USER is ANSI SQL standard, while USER_NAME() is a SQL Server function. Both return the database user name.
Does CURRENT_USER work in Azure SQL Database?
Yes, it works the same way. In Azure SQL Database, the database user is often the login name, but contained users are also supported.
Can I use CURRENT_USER in a DEFAULT constraint?
No, because it is non-deterministic. You can use it in a trigger or stored procedure to set values dynamically.
Why does CURRENT_USER return 'dbo' when I am a sysadmin?
Sysadmins are mapped to the dbo user in each database by default, so CURRENT_USER returns dbo. Use SUSER_SNAME() to see the login.
Is CURRENT_USER the same as ORIGINAL_LOGIN()?
No. CURRENT_USER returns the database user in the current context, which can change with EXECUTE AS. ORIGINAL_LOGIN() returns the login that originally connected, regardless of impersonation.
Can I use CURRENT_USER in a WHERE clause with a parameter?
Yes, it is a scalar function and can be used anywhere an expression is allowed, including WHERE, SELECT, and HAVING.