问题描述
我有一个基本的 SQL 查询,开头是:
I have a basic SQL query, starting with:
SELECT top 20 application_id, [name], location_id FROM apps
现在,我想完成它以便它做到这一点(用伪代码编写)
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
<小时>
根据要求,这是一个例子
As requested, here is an example
application_id name location_id
----------------------------------------------------------
1 Joe Blogs 33
2 Sam Smith 234
3 Jeremy Carr 33
@locid 是用户给出的结果,例如'33, 234'
@locid is the results given by the user, for example '33, 234'
如果@lid 为空,那么我希望它输出带有名称和application_id 的location_id 的所有行.否则,我希望它输出与@lid 中提供的数字相关的所有行(代表 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
干杯
这篇关于如何在 T-SQL 中使用逗号分隔的值列表作为过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)