insert into table select getdate(),(select max(id) from table)+day(getdate())
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
怎样在插入数据时使receiptID等于ID和date列中日期(day)的和?id+cast(day(getdate()) as int)? 疑问: 那你的date列中都为当日日期吗?
select getdate(),(select max(id) from table)+day(getdate())
(
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
疑问:
那你的date列中都为当日日期吗?