我隻想說以下是很基礎的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
看上去沒問題吧,但是就會報錯,錯誤提示如下:
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)
看上去也沒有問題吧,但是把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/本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,如有問題,可以通過以下郵箱位址
[email protected]聯系我,非常感謝。