如何给这个datetime型字段设置默认值 默认值为当前时间+100年

wenyongjie 2008-12-30 09:47:48
新建一个datetime字段,主要是记录产品使用期限,默认值为当前时间+100年,但我不知怎么设置?有哪个知道,说下,谢谢

DateAdd(Year,100,GetDate())
...全文
183 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
特别 2008-12-30
  • 打赏
  • 举报
回复
你这样是正确的,
insert into table (field1,field2,enddate)
values
('abc','123',DateAdd(Year,100,GetDate()))
dawugui 2008-12-30
  • 打赏
  • 举报
回复
create table tb(id int, dt datetime default dateadd(year,100,getdate()))
insert into tb(id) values(1)
insert into tb(id) values(2)
insert into tb(id) values(3)
insert into tb(id) values(4)
insert into tb(id) values(5)
go

select * from tb

drop table tb

/*
id dt
----------- ------------------------------------------------------
1 2108-12-30 09:54:39.060
2 2108-12-30 09:54:39.060
3 2108-12-30 09:54:39.060
4 2108-12-30 09:54:39.060
5 2108-12-30 09:54:39.060

(所影响的行数为 5 行)

*/
水族杰纶 2008-12-30
  • 打赏
  • 举报
回复
if object_id('tempdb..#')is not null drop table #
go
create table #([name] varchar(10) primary key)
insert # select 'A'
insert # select 'D'
insert # select 'C'
insert # select 'B'
alter table #
add [date] datetime default dateadd(year,100,getdate())
go
insert #([name]) select 'E'
select * from #
/*name date
---------- ------------------------------------------------------
A NULL
B NULL
C NULL
D NULL
E 2108-12-30 09:51:21.467*/

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧