2017-11-03 4 views
0

가 나는 등의 데이터 가져 오기 위해 SQL 쿼리 쓰고 싶다 SQL의 조건 :부울 절

1. when param = 'all' it should list data across the table 
2. when param = 'yes' it should list data where invoicenumber is not empty. 
3. when param = 'no' it should list data where invoicenumber is empty. 
내가 예에 대한 쿼리 아래 시도

을 더

declare @invoiced as nvarchar(10) = 'no' 
select * from OrderSummary 
    where 
((@invoiced = 'yes') or (InvoiceNumber = '')) 
    and 
((@invoiced = 'no') or (InvoiceNumber <> '')) 

는 지금은 모든 통합 싶지 조건, 아무도 내가

답변

2
declare @invoiced as nvarchar(10) = 'no' 
select * from OrderSummary 
    where 
    @invoiced = 'all' 
    OR 
    (@invoiced = 'yes' AND InvoiceNumber <> '') 
    OR 
    (@invoiced = 'no' AND InvoiceNumber = '') 
0

을 시도 것을 달성 할 수있는 방법을 제안 할 수있다3210
declare @invoiced as nvarchar(10) = 'no' 
select 
    * 
    from OrderSummary 
    where 
    (
    @invoiced = 'all' 
    OR 
    (
     @invoiced = 'yes' 
     AND 
     InvoiceNumber <> '' 
    ) 
    OR 
    (
     @invoiced = 'no' 
     AND 
     InvoiceNumber = '' 
    ) 
) 
0
declare @invoiced as nvarchar(10) = 'no' 
select * from OrderSummary 
    where 
((@invoiced = 'yes') and (InvoiceNumber <> '')) 
    or 
((@invoiced = 'no') and ((InvoiceNumber = '') or (InvoiceNumber = null))) 
or (@invoiced = 'all') 

위 쿼리로이 쿼리를 업데이트하십시오.

+0

위 검색어로이 검색어를 업데이트하십시오. –

0

귀하의 요구 사항을 충족해야합니다.

declare @invoiced as nvarchar(10) = 'no' 

select * from OrderSummary 
    where 
((@invoiced in ('all','no')) OR (@invoiced = 'yes' AND InvoiceNumber <> '')) 
    and 
((@invoiced in ('all','yes')) OR (@invoiced = 'no' AND InvoiceNumber = '')) 
    and 
(@invoiced in ('no','yes'))