新建一个datetime字段,主要是记录产品使用期限,默认值为当前时间+100年,但我不知怎么设置?有哪个知道,说下,谢谢DateAdd(Year,100,GetDate())

解决方案 »

  1.   

    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*/
      

  2.   

    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)
    goselect * from tbdrop 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 行)*/
      

  3.   

    你这样是正确的,
    insert into table (field1,field2,enddate) 
    values
    ('abc','123',DateAdd(Year,100,GetDate()))