这里when是用在哪里的?会不会式 when 的错误,改为 wherewhen datediff([n],starttime,getdate()) % spatime=0 and datediff([n],starttime,getdate())<>0

解决方案 »

  1.   

    where子句加一个条件" and starttime<>getdate()"
      

  2.   

    where子句加一个条件"datediff([n],starttime,getdate())<>0"
      

  3.   

    完整的语句如下
    sqltime = "update fenji Set Flags = b.Flag from fenji a inner join ( select pointname,flag=case  when (datediff( [n],starttime,getdate()) % spatime =1)  then 1 else 0 end   From ( select a.pointname,a.starttime,convert(datetime,max(b.xdate+' '+b.xtime),120) as xtime,a.spatime from fenji a inner join xinxi b on a.pointname=b.pointname and a.starttime<getdate() group by a.pointname,a.starttime,a.spatime )t )b on a.PointName=b.PointName"如果我加上where datediff([n],starttime,getdate())<>0 总是会报错
      

  4.   

    你还没有发楼上的帖子的时候,我做了一个测试
    create table #t (DT datetime,DES varchar(10))insert into #t values('2005-07-24','7-24')
    insert into #t values('2005-07-25','7-25')select * from #t
    DT                                                     DES        
    ------------------------------------------------------ ---------- 
    2005-07-24 00:00:00.000                                7-24
    2005-07-25 00:00:00.000                                7-25(2 row(s) affected)
    select 'Day' = 
    case when datediff([n],dt,datediff(n,'2005-07-25 00:00:00.000',convert(varchar(20),convert(datetime,convert(varchar(10),getdate(),120)),120))) % 3600 = 0
    and datediff([n],dt,datediff(n,'2005-07-25 00:00:00.000',convert(varchar(20),convert(datetime,convert(varchar(10),getdate(),120)),120))) <> 0 then 'today'
    else 'the other day'
    end,des,datediff([n],dt,getdate()),dt
     from #t
    /*
    Day           des                    dt                                                     
    ------------- ---------- ----------- ------------------------------------------------------ 
    today         7-24       2364        2005-07-24 00:00:00.000
    the other day 7-25       924         2005-07-25 00:00:00.000(2 row(s) affected)
    */从我的这个测试数据,你也应该看到问题,你用datadiff是计算相差的分钟,你用getdate()来比较,每过一分钟,你的结果就不一样,你看:
    print datediff(n,'2005-07-25 00:00:00.000',convert(varchar(20),convert(datetime,convert(varchar(10),getdate(),120)),120))
    print datediff(n,'2005-07-25 00:00:00.000',getdate())
    print convert(varchar(20),getdate(),120)
    /*
    0
    929
    2005-07-25 15:29:06
    */
    第一个只是取出今天的年月日来和'2005-07-25 00:00:00.000'比较,是相等,但是你用getdate()来比较,就是不一样的,所以你用的when datediff([n],starttime,getdate()) % spatime=0  and datediff([n],starttime,getdate())<>0肯定是有起作用,
    你用case  ....当然是搭配用when
    应该是
    flag=case  when (datediff( [n],starttime,getdate()) % spatime =1 --不是说余等于0吗
    )  then 1 else 0 end   
    改为
    flag=case  when (datediff( [n],starttime,getdate()) % spatime =0
     and datediff([n],starttime,getdate())<>0 )  then 1 
    else 0 end ---你原来用的就是对的
    你要check你的数据,到底相等吗?你用的是分钟比较,例如
    print datediff(n,'2005-07-25 00:00:00.000',convert(varchar(20),convert(datetime,convert(varchar(10),getdate(),120)),120))
    print datediff(n,'2005-07-25 00:00:00.000',getdate())
    结果是不一样的,你要自己算算到底相等不?
    getdate()每过一分钟datadiff就不一样,你的语句有陷阱~
      

  5.   

    首先非常感谢早茶帮忙
    我试过用flag=case  when (datediff( [n],starttime,getdate()) % spatime =0
     and datediff([n],starttime,getdate())<>0 )  then 1 else 0 end ---但是结果和没有修改前是一样的,没有变化
    原来的贴子地址是http://community.csdn.net/Expert/topic/4056/4056872.xml?temp=.7971918其中第二个问题,如果可以的话请你去看一下,可能有助于问题的解决,或者抛开我现在的这种写法,有没有别的解决办法?
    原来是打算取余等于0的,但是经过一段时间的思考后觉得还是=1比较好
      

  6.   

    case when 语句是不是只能有一个条件,我的意思是是否可以写成这样
    case when a=b and c=d then,我看了很多贴子都是只有一个条件
      

  7.   

    可以写成 case when exp1 and exp2 的形式!
    when datediff([n],starttime,getdate()) % spatime=0  and datediff([n],starttime,getdate())<>0 ,还有一个小问题,应该把 and 后面的条件放在前面