我的sp有一个日期参数的入参@datein
行情表a如下:
stockid price date
先是select * from a where date>@datein
但是这时取出的price就是date对应的值
但要求是要取出的price是date的前一天,也就是dateadd(day,-1,@datein)的值,这个怎么取啊?
行情表a如下:
stockid price date
先是select * from a where date>@datein
但是这时取出的price就是date对应的值
但要求是要取出的price是date的前一天,也就是dateadd(day,-1,@datein)的值,这个怎么取啊?
select * from a where datediff(day,date,@datein)=1
我的意思是如果表里面的记录如下
stockid price date
1 10 20100525
1 11 20100526
1 12 20100527
1 13 20100528
2 20 20100525
2 21 20100526
2 22 20100527
2 23 20100528我传入的datein参数为20100527
那么要取出的记录为1 11 201005271 12 201005282 21 201005272 22 20100528
也就是对应的price,是当天的前一天的记录。
然后把这些记录的price更新为他们前一天的记录对应的price的值,
其实就是分着2步走的意思也就是先取出
1 12 20100527
1 13 20100528
2 22 20100527
2 23 20100528
然后把price更新为它们对应的日期的前一天的那个price的值
你的where条件里面取的还是date值和我传入的@datein只差一天的记录啊
实际上如果我传入的时期是20100501,今天是20100528
那么从1号到28号的所有记录我都需要取出来
然后再把这所有记录的price值更新为它们各自的日期的前一天的price值
create function getBeforeValue(@stockid int,@date date)
returns int
as
begin
declare @i int
set @i=0
select @i=price from tablename where stockid =@stockid and date=dateadd(day,-1,@datein)
return @i
endcreate function getTable(@datein date)
returns table
as
return select stockid,dbo.getBeforeValue(stockid,date),date from tablename where date >=@datein;--执行
select getTable('20100527')
消息 207,级别 16,状态 1,第 5 行
列名 'date' 无效。
from tablename tt
where tt.date >=@datein