問題描述
我有一個基本的 SQL 查詢,開頭是:
I have a basic SQL query, starting with:
SELECT top 20 application_id, [name], location_id FROM apps
現(xiàn)在,我想完成它以便它做到這一點(用偽代碼編寫)
Now, I would like to finish it so that it does this (written in Pseudocode)
if @lid > 0 then
WHERE location_id IN (@lid)
else
WHERE location_id is all values in location_id column
<小時>
根據(jù)要求,這是一個例子
As requested, here is an example
application_id name location_id
----------------------------------------------------------
1 Joe Blogs 33
2 Sam Smith 234
3 Jeremy Carr 33
@locid 是用戶給出的結(jié)果,例如'33, 234'
@locid is the results given by the user, for example '33, 234'
如果@lid 為空,那么我希望它輸出帶有名稱和application_id 的location_id 的所有行.否則,我希望它輸出與@lid 中提供的數(shù)字相關(guān)的所有行(代表 location_id.
If @lid is empty then I'd like it to output all rows for location_id with name and application_id. Otherwise, I'd like it to output all rows in relation to the provided numbers in @lid (standing for location_id.
所以,如果@lid 為 0:
So, if @lid is 0:
application_id name location_id
----------------------------------------------------------
1 Joe Blogs 33
2 Sam Smith 234
3 Jeremy Carr 33
否則,如果@lid 包含'33'
Otherwise, if @lid contains '33'
application_id name location_id
----------------------------------------------------------
1 Joe Blogs 33
3 Jeremy Carr 33
推薦答案
嘗試使用 Case,它用于 IIF 或三元運算符.請檢查此鏈接 http://msdn.microsoft.com/en-us/圖書館/ms181765.aspx
Try using Case, which serves the purpose of an IIF or a ternary operator. Please check this link http://msdn.microsoft.com/en-us/library/ms181765.aspx
干杯
這篇關(guān)于如何在 T-SQL 中使用逗號分隔的值列表作為過濾器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!