當未指定默認值時,允許將系統(tǒng)為當前用戶的數(shù)據(jù)庫用戶名提供的值插入表內(nèi)。
USER
char
USER 提供與 USER_NAME 系統(tǒng)函數(shù)相同的功能。
在 CREATE TABLE 或 ALTER TABLE 語句中將 USER 和 DEFAULT 約束一起使用,或者將 USER 作為任何標準函數(shù)使用。
本示例聲明一個 char 類型的變量,將 USER 的當前值賦給它,然后打印該變量以及文本說明。
DECLARE @usr char(30)
SET @usr = user
SELECT 'The current user's database username is: '+ @usr
GO
下面是結(jié)果集:
-----------------------------------------------------------------------
The current user's database username is: dbo
(1 row(s) affected)
本示例生成一個表,將 USER 用作銷售行的銷售員的 DEFAULT 約束。
USE pubs
GO
CREATE TABLE inventory2
(
part_id int IDENTITY(100, 1) NOT NULL,
description varchar(30) NOT NULL,
entry_person varchar(30) NOT NULL DEFAULT USER
)
GO
INSERT inventory2 (description)
VALUES ('Red pencil')
INSERT inventory2 (description)
VALUES ('Blue pencil')
INSERT inventory2 (description)
VALUES ('Green pencil')
INSERT inventory2 (description)
VALUES ('Black pencil')
INSERT inventory2 (description)
VALUES ('Yellow pencil')
GO
下面是從表 inventory2 中選擇所有信息的查詢:
SELECT *
FROM inventory2
ORDER BY part_id
GO
下面是結(jié)果集(注意 entry-person 的值):
part_id description entry_person
----------- ------------------------------ -----------------------------
100 Red pencil dbo
101 Blue pencil dbo
102 Green pencil dbo
103 Black pencil dbo
104 Yellow pencil dbo
(5 row(s) affected)
相關(guān)文章