返回當(dāng)前的用戶。此函數(shù)等價(jià)于 USER_NAME()。
CURRENT_USER
sysname
下面的示例將一個(gè)變量聲明為 char,并將 CURRENT_USER 的當(dāng)前值指派給它,然后返回該變量,返回時(shí)還帶有一個(gè)文本描述。
SELECT 'The current user is: '+ convert(char(30), CURRENT_USER)
下面是結(jié)果集:
---------------------------------------------------
The current user is: dbo
(1 row(s) affected)
下面的示例創(chuàng)建一個(gè)表,該表針對銷售行的 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
下面是結(jié)果集:
order_id cust_id order_date order_amt order_person
----------- ----------- ------------------- ------------- --------------
1000 5105 Mar 4 1998 10:13AM 577.95 dbo
(1 row(s) affected)
相關(guān)文章