SQL Keywords

SQL SYSTEM_USER

What is SQL SYSTEM_USER?

SYSTEM_USER returns the name of the login or operating-system account executing the current session.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL SYSTEM_USER: SQL Server (all editions), Azure SQL Database, MySQL/MariaDB (as SYSTEM_USER()), Oracle (v$session.USERNAME provides similar), not available in PostgreSQL or SQLite

SQL SYSTEM_USER Full Explanation

SYSTEM_USER is a built-in, zero-argument scalar function that reports the identity under which the current connection is authenticated. In SQL Server it returns the login name for SQL logins or the Windows domain\username for Windows-authenticated logins. Because it reflects the security context of the connection, the value is constant for the lifetime of the session and is unaffected by database context changes or SET USER_NAME statements.The function is evaluated on the server, making it immune to client-side spoofing. It is non-deterministic from a query-optimizer standpoint (its value can differ between sessions) yet deterministic within a single session.Unlike CURRENT_USER or SESSION_USER, which can be influenced by EXECUTE AS or SETUSER, SYSTEM_USER always reports the original login context. In MySQL, SYSTEM_USER() is a synonym for CURRENT_USER(), returning user@host. PostgreSQL and SQLite do not implement SYSTEM_USER, but provide CURRENT_USER and SESSION_USER as alternatives.Typical uses include auditing (stamping rows with the login that performed an operation), conditional logic in stored procedures, and troubleshooting permission issues. Because the returned string length and format depend on the authentication method, designers should use NVARCHAR(128) or similar columns to store it.Caveats:- Returns one string only; does not separate domain and account into different columns.- Cannot be used in CHECK constraints in Azure SQL Database.- For contained databases in SQL Server, returns the contained database user name when using contained database authentication.

SQL SYSTEM_USER Syntax

SELECT SYSTEM_USER;

SQL SYSTEM_USER Parameters

Example Queries Using SQL SYSTEM_USER

-- Audit who ran a query
SELECT SYSTEM_USER AS executor;

-- Insert a created_by column automatically
INSERT INTO orders (order_id, created_by)
VALUES (1001, SYSTEM_USER);

-- Compare SYSTEM_USER with CURRENT_USER
SELECT SYSTEM_USER   AS login_name,
       CURRENT_USER  AS database_user;

Expected Output Using SQL SYSTEM_USER

  • Each statement returns a single NVARCHAR value such as 'ACME
  • jdoe' or 'sql_admin'
  • In the INSERT example, the created_by column stores that value for future reference

Use Cases with SQL SYSTEM_USER

  • Capture the login that inserted or updated a row for audit trails
  • Display the connected user in application status panels
  • Troubleshoot authentication by comparing SYSTEM_USER with CURRENT_USER
  • Conditional logic in stored procedures that branch on login name

Common Mistakes with SQL SYSTEM_USER

  • Assuming SYSTEM_USER changes after EXECUTE AS; it does not
  • Using it where CURRENT_USER is required (database principal vs login)
  • Expecting it to exist in PostgreSQL or SQLite without a compatibility layer

Related Topics

CURRENT_USER, SESSION_USER, USER_NAME(), SUSER_SNAME(), ORIGINAL_LOGIN()

First Introduced In

SQL Server 2000

Frequently Asked Questions

What does SQL SYSTEM_USER return?

SYSTEM_USER outputs the login or operating-system account that established the current database connection.

Is SYSTEM_USER affected by EXECUTE AS?

No. It always reports the original login, ignoring impersonation done with EXECUTE AS or SETUSER.

Can I store SYSTEM_USER in a table default?

Yes. Define the column with `DEFAULT (SYSTEM_USER)` to automatically capture the login name on INSERT.

Is SYSTEM_USER available in MySQL?

Yes, MySQL implements SYSTEM_USER() as a synonym for CURRENT_USER(), returning 'user@host' for the connection.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!