表如下:
表A
barcode        inserttime            
1234567         2009-08-08
7654321         2009-08-09
5678901         2009-08-10
表B           注:周期是维护周期,既每间隔多少天维护一次
barcode        zhouqi
1234567          1
7654321          2
5678901          3
现在求:   表A的条码(barcode),在加上指定的维护日期后与当前时间对比,如果超过了当前日期,读取出来
既:
1234567 需要把他的inseertime  2009-08-08加上他的zhouqi :1然后与当前时间对比getdate()小弟暂时写出如下:
select * from wupin where DateAdd(dd,3,lasttime)>getdate(), 主要中间的那个3天?1天?2天,怎么确认相加?

解决方案 »

  1.   

    相差几天用DATEDIFF(DD,DATE1,DATE2)
      

  2.   


    修改了你的数据-- =============================================
    -- Author:      T.O.P
    -- Create date: 2009/11/28
    -- Version:     SQL SERVER 2005
    -- =============================================
    declare @TB1 table([barcode] int,[inserttime] datetime)
    insert @TB1
    select 1234567,'2009-11-26' union all
    select 7654321,'2009-11-27' union all
    select 5678901,'2009-11-28'declare @TB2 table([barcode] int,[zhouqi] int)
    insert @TB2
    select 1234567,1 union all
    select 7654321,2 union all
    select 5678901,3SELECT A.*
    FROM @TB1 A INNER JOIN @TB2 B ON A.BARCODE = B.BARCODE
    WHERE DATEADD(DD,ZHOUQI,[inserttime])>GETDATE()--测试结果:
    /*
    barcode     inserttime
    ----------- -----------------------
    7654321     2009-11-27 00:00:00.000
    5678901     2009-11-28 00:00:00.000(2 row(s) affected)
    */
      

  3.   


    create table #A(barcode int, inserttime datetime);
    insert into #A
    select 1234567 , '2009-08-08 ' union all 
    select 7654321 , '2009-08-09 ' union all 
    select 5678901 , '2009-11-26'create table #B(barcode int, zhouqi int);
    insert into #B
    select 1234567 , 1 union all 
    select 7654321 , 2 union all 
    select 5678901 , 3
    select #A.* from #A join #B on #A.barcode = #B.barcode 
    where datediff(day, dateadd(day, #B.zhouqi, #A.inserttime), getdate()) < 0/*
    结果:
    barcode     inserttime
    ----------- -----------------------
    5678901     2009-11-26 00:00:00.000(1 行受影响)
    */