首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用SQL Server查询将多个select语句合并为一个语句

使用SQL Server查询将多个select语句合并为一个语句
EN

Stack Overflow用户
提问于 2019-06-25 04:06:15
回答 1查看 36关注 0票数 -1

我想使用以下逻辑查询我的SQL Server DB中的一个表:

declare @crntusrdept nvarchar(128)
set @crntusrdept = 'A'

if @crntusrdept includes ('A', 'B', 'C') then (select * from comps where dept in ('A', 'B', 'C'))
if @crntusrdept includes ('D','E') then (select * from comps where dept in ('D','E'))
if @crntusrdept includes ('F', 'G') then (select * from comps where dept in ('F', 'G'))
if @crntusrdept includes ('H') then (select * from comps where dept in ('H'))
EN

回答 1

Stack Overflow用户

发布于 2019-06-25 04:10:45

您可以执行以下操作:

select * 
from comps 
where @crntusrdept in ('A', 'B', 'C') and dept in ('A', 'B', 'C')
   or @crntusrdept in ('D', 'E') and dept in ('D', 'E')
   or @crntusrdept in ('F', 'G') and dept in ('F', 'G')
   or @crntusrdept in ('H') and dept in ('H')
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56743281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档