当前位置: 代码迷 >> Sql Server >> 怎么自动取得当前的日期
  详细解决方案

怎么自动取得当前的日期

热度:146   发布时间:2016-04-27 19:34:10.0
如何自动取得当前的日期
在表中有一字段day,是datetime字段,我想在insert一条新的纪录的时候,这个字段的日期能自动通过当前日期填充,不知道怎么实现?谢谢

------解决方案--------------------
设置默认值为:

getdate()
------解决方案--------------------
1.设置
将该字段的默认值设置为getdate()

2.插入
insert into tb ([day]) values(getdate())
------解决方案--------------------
create table [表名] (
[id] [int] not null ,
[day] [datetime] null constraint [df_表名_day] default (getdate()),
constraint [pk_表名] primary key clustered 
(
[id]
) on [primary] 
)
go


insert into 表名 (id) values(1)
insert into 表名 (id) values(2)

select * from 表名

drop table 表名
  相关解决方案