回答

收藏

使用当前日期列为默认值

技术问答 技术问答 577 人阅读 | 0 人回复 | 2023-09-14

有没有办法将列的默认值设置为DateTime.NowSql Server中?
1 d$ U+ V, Y1 U例子:- A9 V. N$ A5 c' Q! E% P# y( E
table EventId int (auto-increment) not nullDescription nvarchar(50) not nullDate datetime not null该行:- e+ h1 b: s3 _; d3 v
Insert into Event(Description) values('teste');并且要插入一行Date值应为当前日期。
+ P7 y0 C4 R! o1 L- t2 Q( a                                                                2 }1 s5 t) E! Q
    解决方案:                                                               
$ f' j: @( ^" o; u7 |8 \- V+ s                                                                使用GETDATE()函数作为值增加默认约束。/ _6 e1 o6 b4 X4 Z( j' R; J( H& b
ALTER TABLE myTable   ADD CONSTRAINT CONSTRAINT_NAME    DEFAULT GETDATE() FOR myColumn
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则