SQL Server SESSION_USER() Function

Definition and Usage

The SESSION_USER function in SQL Server returns the name of the currently connected user to the database. This is useful for security and auditing purposes, allowing you to identify who is executing a particular query or making changes within the database.

Syntax

Syntax

SESSION_USER
            

The function doesn't take any arguments; it simply returns the current user's name.

Example

Example: Getting the Current User

This simple query demonstrates how to retrieve the current user's name using the SESSION_USER function.

SQL Query

SELECT SESSION_USER;
            
Output

-- The output will be the name of the currently logged-in user.  For example:
--  SESSION_USER
--  ------------------------------
--  your_username_here
            

Technical Details

The SESSION_USER function is supported in SQL Server 2008 and later, as well as Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse.