zoukankan      html  css  js  c++  java
  • SQL点滴29—错误无处不在

    我只想说以下是很基础的sql知识,但是很容易犯错。所以睁大我们的眼睛,屏住我们的呼吸,小心的检查吧!
     
    案例1
    if not exists (select OrderID from CorpEmailSendQueue where orderid=600643425)
    begin
        exec sp3_CorpEmailSendQueue_i @ID=NULL,@OrderID=600643425, @OrderType='F', @EmailType='-2',@ResendTime=0,@SendTime=NULL,@CurrentStatus='u',@GenerateTime=NULL
    end

    上面这个是最终正确的写法,看上去很简单吧,但是有些地方是容易犯错的。

     

    错误1

    if not exists select count(1) from CorpEmailSendQueue where orderid=600643425
    begin
        exec sp3_CorpEmailSendQueue_i @ID=NULL,@OrderID=600643425, @OrderType='F', @EmailType='-2',@ResendTime=0,@SendTime=NULL,@CurrentStatus='u',@GenerateTime=NULL
    end

    看上去没问题吧,但是就会报错,错误提示如下:

    Msg 156, Level 15, State 1, Line 1
    Incorrect syntax near the keyword 'select'. 需要使用园括号把select count(1) from CorpEmailSendQueue where orderid=600643425包起来。

     

    错误2

    if not exists (select count(1) from CorpEmailSendQueue where orderid=600643425)
    begin
        exec sp3_CorpEmailSendQueue_i @ID=NULL,@OrderID=600643425, @OrderType='F', @EmailType='-2',@ResendTime=0,@SendTime=NULL,@CurrentStatus='u',@GenerateTime=NULL
    end

    看上去也没有问题吧,但是把OrderID换成count(1)之后exists就不起作用了,select count(1) from CorpEmailSendQueue where orderid=600643425 在任何情况下都是有值的,我的意思是即使是0也是exists的,所以任何情况下都不会执行下面的存储过程。

    案例2

    select datediff(hh,'2013-11-21 16:50','2013-11-21 17:35') 这个得到的结果居然是1,我再想sql server是不是抽风了,这两个时间之间相差45分钟,还不到1小时,无奈只能使用select datediff(mi,'2013-11-21 16:50','2013-11-21 17:35')得到的结果是45。

    还有个小知识,如果要得到当前时间可以用getdate(),如果是utc时间呢,就是当前时间减8,可以使用getutcdate()。

    作者:Tyler Ning
    出处:http://www.cnblogs.com/tylerdonet/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,请微信联系冬天里的一把火

  • 相关阅读:
    Django组件——分页器
    Django与Ajax
    Python常用模块——包&跨模块代码调用
    Python常用模块——正则表达式re模块
    Python常用模块——文件复制模块shutil
    Python常用模块——hashlib加密
    git小乌龟配置
    设计模式学习(27)- MVC模式
    设计模式学习(26)- 访问者模式
    设计模式学习(25)- 模板模式
  • 原文地址:https://www.cnblogs.com/tylerdonet/p/2934557.html
Copyright © 2011-2022 走看看