簡體   English   中英

使用SQL Server查詢將多個選擇語句合二為一

[英]Make multiple select statements into one using SQL Server Query

我想使用以下邏輯在我的SQL Server數據庫中查詢一個表:

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'))

你可以做:

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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM