0

我想写一个 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 <> ''))

现在我也想合并所有条件,任何人都可以建议我如何实现这一点

4

4 回答 4

2
declare @invoiced as nvarchar(10) = 'no'
select * from OrderSummary 
    where 
    @invoiced = 'all'
    OR 
    (@invoiced = 'yes' AND InvoiceNumber <> '')
    OR 
    (@invoiced = 'no' AND InvoiceNumber = '')
于 2017-11-03T06:53:49.847 回答
0

尝试这个

declare @invoiced as nvarchar(10) = 'no'
select 
  * 
  from OrderSummary 
  where 
  (
     @invoiced = 'all'
     OR
     (
        @invoiced = 'yes'
        AND
        InvoiceNumber <> ''
     )
     OR
     (
        @invoiced = 'no'
        AND
        InvoiceNumber = ''
     )
  )
于 2017-11-03T06:56:30.193 回答
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')

请使用上述查询更新此查询。

于 2017-11-03T06:57:26.200 回答
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'))
于 2017-11-03T07:00:46.470 回答