Transact-SQL 参考

CURRENT_USER

返回当前的用户。此函数等价于 USER_NAME()。

语法

CURRENT_USER

返回类型

sysname

示例
A. 使用 CURRENT_USER 返回当前的用户名

下面的示例将一个变量声明为 char,并将 CURRENT_USER 的当前值指派给它,然后返回该变量,返回时还带有一个文本描述。

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

下面是结果集:

--------------------------------------------------- 
The current user is: dbo                            

(1 row(s) affected)
B. 将 CURRENT_USER 用作 DEFAULT 约束

下面的示例创建一个表,该表针对销售行的 order_person 列将 CURRENT_USER 用作 DEFAULT 约束。

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

下面的查询从 orders2 表中选择所有信息。

SELECT * 
FROM orders2

下面是结果集:

order_id    cust_id     order_date             order_amt    order_person                   
----------- ----------- ------------------- ------------- -------------- 
1000        5105        Mar 4 1998 10:13AM      577.95           dbo                            

(1 row(s) affected)

请参见

ALTER TABLE

CREATE TABLE

系统函数