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

使用 IS NULL 或 Coalesce 將參數(shù)傳遞給 where 子句

Passing in parameter to where clause using IS NULL or Coalesce(使用 IS NULL 或 Coalesce 將參數(shù)傳遞給 where 子句)
本文介紹了使用 IS NULL 或 Coalesce 將參數(shù)傳遞給 where 子句的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想將參數(shù) @CompanyID 傳入 where 子句以過濾結(jié)果.但有時(shí)這個(gè)值可能是 null 所以我希望返回所有記錄.我找到了兩種方法,但不確定哪一種最安全.

I would like to pass in a parameter @CompanyID into a where clause to filter results. But sometimes this value may be null so I want all records to be returned. I have found two ways of doing this, but am not sure which one is the safest.

版本 1

SELECT ProductName, CompanyID 
FROM Products 
WHERE (@CompanyID IS NULL OR CompanyID = @CompanyID)

版本 2

SELECT ProductName, CompanyID 
FROM Products 
WHERE CompanyID = COALESCE(@CompanyID, CompanyID)

我發(fā)現(xiàn)第一個(gè)版本是最快的,但我也發(fā)現(xiàn)在其他表中使用類似的方法我得到了不同的結(jié)果集.我不太明白兩者之間的區(qū)別.

I have found that the first version is the quickest, but I have also found in other tables using a similar method that I get different result sets back. I don't quite understand the different between the two.

誰能解釋一下?

推薦答案

好吧,兩個(gè)查詢都在處理相同的兩個(gè)場(chǎng)景 -
在一種情況下,@CompanyID 包含一個(gè)值,
并且在第二個(gè) @CompanyID 中包含 NULL.

Well, both queries are handling the same two scenarios -
In one scenario @CompanyID contains a value,
and in the second @CompanyID contains NULL.

對(duì)于這兩個(gè)查詢,第一個(gè)場(chǎng)景將返回相同的結(jié)果集 - 因?yàn)槿绻?@CompanyId 包含一個(gè)值,兩者都將返回 companyId = @CompanyId 的所有行,但是第一個(gè)查詢可能會(huì)更快地返回它(在我的答案末尾有更多信息).

For both queries, the first scenario will return the same result set - since if @CompanyId contains a value, both will return all rows where companyId = @CompanyId, however the first query might return it faster (more on that at the end of my answer).

然而,第二種情況是查詢開始表現(xiàn)不同.

The second scenario, however, is where the queries starts to behave differently.

首先,這就是你得到不同結(jié)果集的原因:

First, this is why you get different result sets:

結(jié)果集的差異

版本 1

WHERE (@CompanyID IS NULL OR CompanyID = @CompanyID)

當(dāng)@CompanyID為空時(shí),where子句不會(huì)過濾任何行,返回表中的所有記錄.

When @CompanyID is null, the where clause will not filter out any rows whatsoever, and all the records in the table will be returned.

版本 2

WHERE CompanyID = COALESCE(@CompanyID, CompanyID)

當(dāng)@CompanyID為null時(shí),where子句會(huì)過濾掉CompanyID為null的所有行,因?yàn)?code>null = null的結(jié)果> 實(shí)際上是 unknown - 任何帶有 null = null 的查詢都不會(huì)返回任何結(jié)果,除非 ANSI_NULLS 設(shè)置為 OFF(你真的不應(yīng)該這樣做,因?yàn)樗驯粭売?.

When @CompanyID is null, the where clause will filter out all the rows where CompanyID is null, since the result of null = null is actually unknown - and any query with null = null as it's where clause will return no results, unless ANSI_NULLS is set to OFF (which you really should not do since it's deprecated).

索引使用

您可能會(huì)從第一個(gè)版本中獲得更快的結(jié)果,因?yàn)樵?where 子句中的列上使用任何函數(shù)都會(huì)阻止 SQL Server 使用您在該列上可能擁有的任何索引.您可以在 這篇文章 MSSql 技巧.

You might get faster results from the first version, since the use of any function on a column in the where clause will prevent SQL Server from using any index that you might have on this column. You can read more about it on this article in MSSql Tips.

結(jié)論

版本 1 優(yōu)于版本 2.即使您不想返回 companyId 為空的記錄,最好寫為 WHERE (@CompanyID IS NULL OR CompanyID = @CompanyID) AND CompanyID IS NOT NULL而不是使用第二個(gè)版本.

Version 1 is better than version 2. Even if you do not want to return records where companyId is null it's still better to write as WHERE (@CompanyID IS NULL OR CompanyID = @CompanyID) AND CompanyID IS NOT NULL than to use the second version.

這篇關(guān)于使用 IS NULL 或 Coalesce 將參數(shù)傳遞給 where 子句的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Modify Existing decimal places info(修改現(xiàn)有小數(shù)位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關(guān)名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號(hào)或管道運(yùn)算符字符串中刪除重復(fù)項(xiàng))
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關(guān)系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 国产日韩欧美在线 | 日韩久久综合 | 亚洲精品9999 | 欧美久久一区 | 日本aaa视频 | 国产色视频网站 | 狠狠涩| 一区二区三区免费观看 | 在线成人免费视频 | 国产真实精品久久二三区 | 一级片毛片 | 国产精品亚洲成在人线 | 日韩在线免费看 | 一区免费 | 成人精品鲁一区一区二区 | 国产精品区一区二区三 | 亚洲国产第一页 | 国产精品一区二区在线播放 | 亚洲一区二区三区四区五区午夜 | 91影院| 中文字幕在线精品 | 久久激情视频 | 亚洲综合激情 | 操到爽 | 国产精品免费看 | 亚洲有码转帖 | 91夜夜夜 | 欧美激情视频一区二区三区免费 | 91精品久久久久久久久久入口 | 欧美亚洲另类丝袜综合网动图 | 中文字幕精品一区 | 亚洲网站免费看 | 欧美精品a∨在线观看不卡 国产精品久久国产精品 | 久久国产精品72免费观看 | 日本成人区 | 国产一区二区精 | 免费在线观看av的网站 | 视频三区 | 国产91网址 | 99久久婷婷国产亚洲终合精品 | 精品欧美乱码久久久久久 |