回答

收藏

使用当前日期列为默认值

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

有没有办法将列的默认值设置为DateTime.NowSql Server中?
, V8 ^" _/ Q8 I3 b! Z, @例子:
9 I* W1 |$ H$ Ctable EventId int (auto-increment) not nullDescription nvarchar(50) not nullDate datetime not null该行:
" U. o. X( J0 h% t$ MInsert into Event(Description) values('teste');并且要插入一行Date值应为当前日期。
: \! ]' U. |/ j+ j                                                                ( w5 x: r+ n9 j6 \# V( j
    解决方案:                                                                  i2 D3 G) w' p
                                                                使用GETDATE()函数作为值增加默认约束。
& \& w+ l4 c5 @* FALTER TABLE myTable   ADD CONSTRAINT CONSTRAINT_NAME    DEFAULT GETDATE() FOR myColumn
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则