SQL Server CURRENT_USER() Function


SQL Server CURRENT_USER() Function

The SQL Server CURRENT_USER() function returns the name of the current user in the SQL Server database. This function is useful for identifying the user currently connected to the database.


Syntax

SELECT CURRENT_USER;

The CURRENT_USER() function does not take any arguments.


Example SQL Server CURRENT_USER() Function Queries

Let's look at some examples of SQL Server CURRENT_USER() function queries:

1. Basic CURRENT_USER() Example

SELECT CURRENT_USER AS current_user;

This query returns the name of the current user. The result will be:

current_user
--------------
username

2. Using CURRENT_USER() in a Table

CREATE TABLE user_actions (
    action_id INT PRIMARY KEY,
    action_description VARCHAR(255),
    user_name VARCHAR(50) DEFAULT CURRENT_USER
);
INSERT INTO user_actions (action_id, action_description) VALUES (1, 'Logged In');
SELECT * FROM user_actions;

This example creates a table named user_actions with a column that defaults to the current user. The result will be:

action_id  action_description  user_name
---------  -------------------  --------------
1          Logged In            username

3. CURRENT_USER() with a Variable

DECLARE @current_user VARCHAR(50);
SET @current_user = CURRENT_USER;
SELECT @current_user AS current_user;

This query uses a variable to store the current user's name and then returns it. The result will be:

current_user
--------------
username

Full Example

Let's go through a complete example that includes creating a table, inserting data, and using the CURRENT_USER() function.

Step 1: Creating a Table

This step involves creating a new table named logs to store some sample data with user actions.

CREATE TABLE logs (
    log_id INT PRIMARY KEY,
    log_message VARCHAR(255),
    log_user VARCHAR(50) DEFAULT CURRENT_USER
);

In this example, we create a table named logs with columns for log_id, log_message, and log_user.

Step 2: Inserting Data into the Table

This step involves inserting some sample data into the logs table.

INSERT INTO logs (log_id, log_message) VALUES (1, 'User logged in');
INSERT INTO logs (log_id, log_message) VALUES (2, 'User viewed a page');
INSERT INTO logs (log_id, log_message) VALUES (3, 'User logged out');

Here, we insert data into the logs table.

Step 3: Using the CURRENT_USER() Function

This step involves using the CURRENT_USER() function to retrieve the current user for each log entry.

SELECT log_id, log_message, log_user
FROM logs;

This query retrieves the log_id, log_message, and log_user for each row in the logs table. The result will be:

log_id  log_message        log_user
------  -------------      --------
1       User logged in     username
2       User viewed a page username
3       User logged out    username

Conclusion

The SQL Server CURRENT_USER() function is a useful tool for identifying the current user in the SQL Server database. Understanding how to use the CURRENT_USER() function and its syntax is essential for effective user management and data processing in SQL Server.