我的sp有一个日期参数的入参@datein
行情表a如下:
stockid price date
先是select * from a where date>@datein
但是这时取出的price就是date对应的值
但要求是要取出的price是date的前一天,也就是dateadd(day,-1,@datein)的值,这个怎么取啊?

解决方案 »

  1.   

    select * from a where date=dateadd(day,-1,@datein)
      

  2.   

    上面的错了,改为:
    select * from a where datediff(day,date,@datein)=1
      

  3.   

    select * from awhere date=@datein-1
      

  4.   

    select * from a where datediff(d,date ,dateadd(day,-1,@datein))=0??
      

  5.   

    谢谢,
    我的意思是如果表里面的记录如下
    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,是当天的前一天的记录。
      

  6.   

    是这样,传入的是20100527,那么我需要取出表中全部的date>=20100527的记录,包括20100527,20102528
    然后把这些记录的price更新为他们前一天的记录对应的price的值,
    其实就是分着2步走的意思也就是先取出
    1 12 20100527
    1 13 20100528
    2 22 20100527
    2 23 20100528
    然后把price更新为它们对应的日期的前一天的那个price的值
      

  7.   

    select stockid,price,dateadd(day,1,date) as date from a where datediff(day,date,@datein)=1
      

  8.   

    to wuxinyuyun:
    你的where条件里面取的还是date值和我传入的@datein只差一天的记录啊
    实际上如果我传入的时期是20100501,今天是20100528
    那么从1号到28号的所有记录我都需要取出来
    然后再把这所有记录的price值更新为它们各自的日期的前一天的price值
      

  9.   

    select stockid,price,dateadd(day,1,date) as date from a where date>@datein
      

  10.   

    --利用函数求解
    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')
      

  11.   

    报错啊,说
    消息 207,级别 16,状态 1,第 5 行
    列名 'date' 无效

      

  12.   

    我懂你的意思了。你其实是还是取出 >@datein的日期的price,只是更改了结果的日期字段,把日期的值+1了
      

  13.   

    select tt.stockid,ttprice=(select price from tablename where stockid=tt.stockid and date=dateadd(day,-1,tt.date),tt.date 
    from tablename tt
    where tt.date >=@datein