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

SQL 使用子查詢分配變量

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

問(wèn)題描述

我對(duì)以下 2 個(gè) SQL 有疑問(wèn):

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

在執(zhí)行之前,我認(rèn)為case部分應(yīng)該是null = 1 or (0 > 0),它會(huì)返回0.

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

但是現(xiàn)在,我想知道為什么第二個(gè) SQL 會(huì)返回 1

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

推薦答案

我會(huì)將此作為答案發(fā)布,因?yàn)樗鼇?lái)自 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

有些人認(rèn)為除非優(yōu)先規(guī)則另有規(guī)定,否則謂詞將從左到右評(píng)估,短路將在可能的情況下進(jìn)行.換句話說(shuō),如果第一個(gè)謂詞propertytype = 'INT' 評(píng)估為 false,SQL Server 不會(huì)評(píng)估第二個(gè)謂詞 CAST(propertyval AS INT) >10 因?yàn)榻Y(jié)果是已經(jīng)知道.基于這個(gè)假設(shè),期望是查詢應(yīng)該永遠(yuǎn)不會(huì)失敗嘗試轉(zhuǎn)換不是敞篷車(chē).

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.

然而,現(xiàn)實(shí)是不同的.SQL Server 可以內(nèi)部支持短路概念;然而,由于一次性概念在語(yǔ)言中,不一定會(huì)按從左到右的順序計(jì)算表達(dá)式.它可以決定,基于成本相關(guān)的原因,從第二個(gè)表達(dá)式開(kāi)始,然后如果第二個(gè)表達(dá)式的計(jì)算結(jié)果為真,則計(jì)算第一個(gè)表達(dá)式也是如此.這意味著如果有行屬性類型不同于INT"的表,并且在這些行中propertyval 不能轉(zhuǎn)換為 INT,查詢可能由于轉(zhuǎn)換錯(cuò)誤.

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.

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

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

相關(guān)文檔推薦

Sql server table usage statistics(Sql server 表使用情況統(tǒng)計(jì))
Relative path in t sql?(t sql中的相對(duì)路徑?)
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 查詢以獲取分層數(shù)據(jù)的 XML 輸出)
T-SQL IF statement embedded in a sum() function(嵌入在 sum() 函數(shù)中的 T-SQL IF 語(yǔ)句)
Table vs Temp Table Performance(表與臨時(shí)表性能)
主站蜘蛛池模板: 黄色在线免费观看 | 欧美黄色网| 日本视频一区二区三区 | 欧美日韩一区二区三区四区 | 亚洲精选一区二区 | 欧洲精品久久久久毛片完整版 | 精精精精xxxx免费视频 | 国产精品一区二区久久 | 国产精品视频一二三 | 久久精品综合 | 亚洲午夜av久久乱码 | 国产精品国产精品国产专区不片 | 色综合天天天天做夜夜夜夜做 | 91精品国产91| 中文字幕一区二区三区精彩视频 | 日韩成人中文字幕 | 天天综合91| 欧美日韩免费 | 成人av播放| www.亚洲一区二区 | 亚洲精品一区二区 | 欧美成人精品激情在线观看 | 国产一区二区不卡 | 国产资源在线观看 | 国产 欧美 日韩 一区 | 亚洲一区二区免费看 | 日韩欧美精品 | 亚州中文| 热99视频 | www97影院 | 国产精品日韩欧美一区二区 | 日韩精品一区二区三区 | 久久av网站 | 在线欧美亚洲 | 国产精品99一区二区 | 亚洲一区中文字幕 | 国产精品久久久久久久久久久新郎 | 亚洲精品99久久久久久 | 欧美成人精品一区 | 国产夜恋视频在线观看 | 亚洲视频一区在线观看 |