当未指定默认值时,允许将系统为当前用户的数据库用户名提供的值插入表内。
USER
char
USER 提供与 USER_NAME 系统函数相同的功能。
在 CREATE TABLE 或 ALTER TABLE 语句中将 USER 和 DEFAULT 约束一起使用,或者将 USER 作为任何标准函数使用。
本示例声明一个 char 类型的变量,将 USER 的当前值赋给它,然后打印该变量以及文本说明。
DECLARE @usr char(30)
SET @usr = user
SELECT 'The current user's database username is: '+ @usr
GO
下面是结果集:
-----------------------------------------------------------------------
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
下面是结果集(注意 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)
相关文章