久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL 使用子查詢分配變量

SQL assign variable with subquery(SQL 使用子查詢分配變量)
本文介紹了SQL 使用子查詢分配變量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我對以下 2 個 SQL 有疑問:

I have a question for following 2 SQL:

declare @i1 bit, @b1 bit
declare @i2 bit, @b2 bit
declare @t table (Seq int)
insert into @t values (1)

-- verify data
select case when (select count(1) from @t n2 where 1 = 2) > 0 then 1 else 0 end
-- result 0

select @i1 = 1, @b1 = case when @i1 = 1 or ((select count(1) from @t n2 where 1 = 2) > 0) then 1 else 0 end from @t n where n.Seq = 1
select @i1, @b1
-- result 1, 0

select @i2 = 1, @b2 = case when @i2 = 1 or (0 > 0) then 1 else 0 end from @t n where n.Seq = 1
select @i2, @b2
-- result 1, 1

SQL Fiddle Here

在執行之前,我認為case部分應該是null = 1 or (0 > 0),它會返回0.

Before the execute, I thought the case part should be null = 1 or (0 > 0), and it will return 0.

但是現在,我想知道為什么第二個 SQL 會返回 1

But now, I wondering why the 2nd SQL will return 1

推薦答案

我會將此作為答案發布,因為它來自 Training Kit (70-461):

I will post this as an answer as it is quite large text from Training Kit (70-461):

WHERE propertytype = 'INT' AND CAST(propertyval AS INT) >10

有些人認為除非優先規則另有規定,否則謂詞將從左到右評估,短路將在可能的情況下進行.換句話說,如果第一個謂詞propertytype = 'INT' 評估為 false,SQL Server 不會評估第二個謂詞 CAST(propertyval AS INT) >10 因為結果是已經知道.基于這個假設,期望是查詢應該永遠不會失敗嘗試轉換不是敞篷車.

Some assume that unless precedence rules dictate otherwise, predicates will be evaluated from left to right, and that short circuiting will take place when possible. In other words, if the first predicate propertytype = 'INT' evaluates to false, SQL Server won’t evaluate the second predicate CAST(propertyval AS INT) > 10 because the result is already known. Based on this assumption, the expectation is that the query should never fail trying to convert something that isn’t convertible.

然而,現實是不同的.SQL Server 可以內部支持短路概念;然而,由于一次性概念在語言中,不一定會按從左到右的順序計算表達式.它可以決定,基于成本相關的原因,從第二個表達式開始,然后如果第二個表達式的計算結果為真,則計算第一個表達式也是如此.這意味著如果有行屬性類型不同于INT"的表,并且在這些行中propertyval 不能轉換為 INT,查詢可能由于轉換錯誤.

The reality, though, is different. SQL Server does internally support a short-circuit concept; however, due to the all-at-once concept in the language, it is not necessarily going to evaluate the expressions in left-to-right order. It could decide, based on cost-related reasons, to start with the second expression, and then if the second expression evaluates to true, to evaluate the first expression as well. This means that if there are rows in the table where propertytype is different than 'INT', and in those rows propertyval isn’t convertible to INT, the query can fail due to a conversion error.

這篇關于SQL 使用子查詢分配變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Sql server table usage statistics(Sql server 表使用情況統計)
Relative path in t sql?(t sql中的相對路徑?)
Getting the last record in SQL in WHERE condition(在 WHERE 條件下獲取 SQL 中的最后一條記錄)
Query to get XML output for hierarchical data using FOR XML PATH in SQL Server(在 SQL Server 中使用 FOR XML PATH 查詢以獲取分層數據的 XML 輸出)
T-SQL IF statement embedded in a sum() function(嵌入在 sum() 函數中的 T-SQL IF 語句)
Table vs Temp Table Performance(表與臨時表性能)
主站蜘蛛池模板: 免费色网址| 81精品国产乱码久久久久久 | 国产精品亚洲精品 | 日韩一二区在线 | 国产精品国产精品 | 国产精品高潮呻吟久久 | 色婷婷av一区二区三区软件 | 久久免费高清视频 | 国产 日韩 欧美 在线 | 日韩在线中文字幕 | 日韩在线国产 | 精品在线观看一区 | 91传媒在线观看 | 国产98色在线 | 日韩 | 欧美成人黄色小说 | 成人性视频免费网站 | 亚洲免费在线 | 黄色毛片黄色毛片 | 亚洲一区成人 | 日日网| 91久久网站| 日韩精品一区在线 | 欧美激情a∨在线视频播放 成人免费共享视频 | 紧缚调教一区二区三区视频 | 日本久久www成人免 成人久久久久 | 美人の美乳で授乳プレイ | 视频在线亚洲 | 亚洲乱码一区二区三区在线观看 | 国产午夜精品一区二区三区嫩草 | www.亚洲 | 中文在线一区二区 | 欧洲视频一区二区 | 天天影视网天天综合色在线播放 | 中国免费黄色片 | 婷婷开心激情综合五月天 | 色爱区综合| 香蕉久久a毛片 | 国产精品片aa在线观看 | 成人免费在线观看 | 91视视频在线观看入口直接观看 | 中文字幕av在线播放 |