title | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic | f1_keywords | helpviewer_keywords | dev_langs | monikerRange | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CURRENT_USER (Transact-SQL) | CURRENT_USER (Transact-SQL) | markingmyname | maghan | 07/24/2017 | sql | t-sql | reference |
|
|
| >= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current |
[!INCLUDE sql-asdb-asdbmi-asa-pdw]
This function returns the name of the current user. This function is equivalent to USER_NAME()
.
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions
CURRENT_USER
sysname
CURRENT_USER
returns the name of the current security context. If CURRENT_USER
executes after a call to EXECUTE AS
switches context, CURRENT_USER
will return the name of the impersonated context. If a Windows principal accessed the database by way of membership in a group, CURRENT_USER
will return the name of the Windows principal instead of the group name.
See SUSER_NAME (Transact-SQL) and SYSTEM_USER (Transact-SQL) to learn about how to return the login of the current user.
This example returns the name of the current user.
SELECTCURRENT_USER; GO
This example creates a table that uses CURRENT_USER
as a DEFAULT
constraint, for the order_person
column, on a sales row.
USE AdventureWorks2022; GO IF EXISTS (SELECT TABLE_NAME FROMINFORMATION_SCHEMA.TABLESWHERE TABLE_NAME ='orders22') DROPTABLE orders22; GO SET NOCOUNT ON; CREATETABLEorders22 ( order_id int IDENTITY(1000, 1) NOT NULL, cust_id intNOT NULL, order_date smalldatetime NOT NULL DEFAULT GETDATE(), order_amt moneyNOT NULL, order_person char(30) NOT NULL DEFAULT CURRENT_USER ); GO
This example inserts a record in the table. The user named Wanida
executes these statements.
INSERT orders22 (cust_id, order_amt) VALUES (5105, 577.95); GO SET NOCOUNT OFF; GO
This query selects all information from the orders22
table.
SELECT*FROM orders22; GO
[!INCLUDEssResult]
order_id cust_id order_date order_amt order_person ----------- ----------- -------------------- ------------ ------------ 1000 5105 2005-04-03 23:34:00 577.95 Wanida (1 row(s) affected)
In this example, user Wanida
executes the following [!INCLUDEtsql] code to impersonate user 'Arnalfo'.
SELECTCURRENT_USER; GO EXECUTE AS USER ='Arnalfo'; GO SELECTCURRENT_USER; GO REVERT; GO SELECTCURRENT_USER; GO
[!INCLUDEssResult]
Wanida Arnalfo Wanida
USER_NAME (Transact-SQL)
SYSTEM_USER (Transact-SQL)
sys.database_principals (Transact-SQL)
ALTER TABLE (Transact-SQL)
CREATE TABLE (Transact-SQL)
System Functions (Transact-SQL)