CURRENT_USER (T-SQL)

Returns the current user. This function is equivalent to USER_NAME().

Syntax

CURRENT_USER

Return Types

sysname

Examples
A. Use CURRENT_USER to return the current username

This example declares a variable as char, assigns the current value of CURRENT_USER to it, and then returns the variable with a text description.

SELECT 'The current user is: '+ convert(char(30), CURRENT_USER)

  

Here is the result set:

---------------------------------------------------

The current user is: dbo                           

  

(1 row(s) affected)

  

B. Use CURRENT_USER as a DEFAULT constraint

    This example creates a table that uses CURRENT_USER as a DEFAULT constraint for the order_person column on a sales row.

    USE pubs

    IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES

            WHERE TABLE_NAME = 'orders2')

        DROP TABLE orders2

    GO

    SET NOCOUNT ON

    CREATE TABLE orders2

    (

     order_id int IDENTITY(1000, 1) NOT NULL,

     cust_id  int NOT NULL,

     order_date datetime NOT NULL DEFAULT GETDATE(),

     order_amt money NOT NULL,

     order_person char(30) NOT NULL DEFAULT CURRENT_USER

    )

    GO

    INSERT orders2 (cust_id, order_amt)

    VALUES (5105, 577.95)

    GO

    SET NOCOUNT OFF

      

    This query selects all information from the orders2 table.

    SELECT *

    FROM orders2

      

    Here is the result set:

    order_id    cust_id     order_date             order_amt    order_person                  

    ----------- ----------- ------------------- ------------- --------------

    1000        5105        Mar 4 1998 10:13AM      577.95           dbo                           

      

    (1 row(s) affected)

      

    See Also
    ALTER TABLE System Functions
    CREATE TABLE  

      


    (c) 1988-98 Microsoft Corporation. All Rights Reserved.