表中有三列,ID(int),date(datetime),receiptID(int),ID列是自动递增,怎样在插入数据时使receiptID等于ID和date列中日期(day)的和?id+cast(day(getdate()) as int)? 最好用存储过程表示。谢谢了!

解决方案 »

  1.   

    insert into table
    select getdate(),(select max(id) from table)+day(getdate())
      

  2.   

    create table test
    (
    id int identity(1,1),
    [date] datetime,
    receiptID int
    )
    insert test select '2006-08-21',22
    select * from test
    go
    create proc addtest
    @date datetime
    as
    begin
    declare @receiptid int
    declare @id int
    select @id=(case when exists(select 1 from test where id=1)
                                        then min(id+1) else 1 end) from test
    select @receiptid=@id+cast(day(getdate()) as int)
    insert test select @date,@receiptid
    end
    go
    exec addtest '2006-08-22'
    select * from test
      

  3.   

    怎样在插入数据时使receiptID等于ID和date列中日期(day)的和?id+cast(day(getdate()) as int)? 
    疑问:
    那你的date列中都为当日日期吗?