在SQL语句中传递NULL作为参数时,如何调整WHERE子句以选择所有不带Premium = 0的记录

时间:2019-03-08 00:01:11

标签: sql-server tsql sql-server-2012

结果应返回:

  • 如果@reasonID = 1仅需要选择具有reasonID = 211的策略
  • 如果@reasonID = 2只需要选择包含reasonID <> 211的{​​{1}}的策略
  • 如果reasonID IS NULL,我需要选择所有策略,包括@reasonID = NULLNULL

以下示例适用于Premium <> 0@reasonID = 1

@reasonID = 1

enter image description here

@reasonID = 2

enter image description here

但是当@reasonID = 2时,如何调整WHERE子句以选择所有行?因为它会返回具有@reasonID = NULL的策略,而我不需要。

@reasonID = NULL

enter image description here

Premium = 0

但是应该这样:

enter image description here

是否可以在不使用declare @TempTable1 table (ControlNo int, PolicyNumber varchar(50), Premium money) insert into @TempTable1 values (1, 'Pol1', 100), (2, 'Pol2', 0), (3, 'Pol3', 50), (4, 'Pol4', 0), (5, 'Pol5', 70), (6, 'Pol6', 0), (7, 'Pol7', 30) declare @TempTable2 table (ControlNo int, PolicyNumber varchar(50), reasonID int) insert into @TempTable2 values (1, 'Pol1', 5), (2, 'Pol2', NULL), (3, 'Pol3', 211), (4, 'Pol4', 8), (5, 'Pol5', 211), (6, 'Pol6', NULL), (7, 'Pol7', 3) --select * from @TempTable1 --select * from @TempTable2 --Here I input @reasonID parameter declare @reasonID int = NULL select T2.ControlNo, T2.PolicyNumber, T1.Premium, T2.reasonID from @TempTable1 T1 inner join @TempTable2 T2 on t1.ControlNo = T2.ControlNo where T1.Premium <> 0 and (case when reasonID = 211 then 1 else 2 end = @reasonID) --works for @reasonID = 1 or @reasonID = 2 or (@reasonID IS NULL) --does not work 子句或WHERE的情况下修改HAVING子句以达到理想的结果?

2 个答案:

答案 0 :(得分:1)

我认为您可能错过了添加一个括号的情况。另外,我在where条件中修改了case语句,因为您的else条件也将考虑空值。既然您具有条件或条件为null,那么现在就没有关系了。我假设您可能不想在case语句中使用null条件,所以我对其进行了更改。我在您的输入中没有看到Policy 8,因此它不会生成8的输出。

declare @TempTable1 table (ControlNo int,PolicyNumber varchar(50), Premium money)
insert into @TempTable1 values (1,'Pol1', 100),
                               (2,'Pol2', 0),
                               (3,'Pol3', 50),
                               (4,'Pol4', 0),
                               (5,'Pol5', 70),
                               (6,'Pol6', 0),
                               (7, 'Pol7',30)

declare @TempTable2 table (ControlNo int,PolicyNumber varchar(50), reasonID int)
insert into @TempTable2 values (1,'Pol1', 5),
                              (2,'Pol2', NULL),
                              (3,'Pol3', 211),
                              (4,'Pol4', 8),
                              (5,'Pol5', 211),
                              (6,'Pol6', NULL),
                              (7,'Pol7',3)
--select * from @TempTable1
--select * from @TempTable2

--Here I input @reasonID  parameter
declare @reasonID int = NULL

select  T2.ControlNo,T2.PolicyNumber, T1.Premium, T2.reasonID 
from    @TempTable1 T1
        inner join @TempTable2 T2 on t1.ControlNo = T2.ControlNo
where   T1.Premium <> 0
        and ((case when reasonID = 211 then 1 
         when isnull(reasonID,'') not in (211,'') then 2 end = @reasonID) --works for @reasonID = 1 or @reasonID = 2
            OR (@reasonID IS NULL)) --does not work (added parentheses)

输出:现在不会带来溢价<> 0

ControlNo   PolicyNumber    Premium reasonID
1            Pol1             100.00    5
3            Pol3              50.00    211
5             Pol5             70.00    211
7             Pol7             30.00    3

答案 1 :(得分:0)

我相信您需要这样的东西:

select *
from @TempTable1 t1
join @TempTable2 t2 on t1.ControlNo = t2.ControlNo
where t1.Premium <> 0
    and (
            (@reasonID is null)
            or
            (@reasonID = 1 and t2.reasonID = 211)
            or
            (@reasonID = 2 and (t2.reasonID <> 211 or t2.reasonID is null))
        )

数据:

declare @TempTable1 table (ControlNo int,PolicyNumber varchar(50), Premium money)
insert into @TempTable1 values (1,'Pol1', 100),
                               (2,'Pol2', 0),
                               (3,'Pol3', 50),
                               (4,'Pol4', 0),
                               (5,'Pol5', 70),
                               (6,'Pol6', 0),
                               (7, 'Pol7',30),
                               (8, 'Pol8',10)

declare @TempTable2 table (ControlNo int,PolicyNumber varchar(50), reasonID int)
insert into @TempTable2 values (1,'Pol1', 5),
                              (2,'Pol2', NULL),
                              (3,'Pol3', 211),
                              (4,'Pol4', 8),
                              (5,'Pol5', 211),
                              (6,'Pol6', NULL),
                              (7,'Pol7',3),
                              (8,'Pol8',null)

对于@reasonID = null

ControlNo   PolicyNumber Premium               ControlNo   PolicyNumber reasonID
----------- ------------ --------------------- ----------- ------------ -----------
1           Pol1         100.00                1           Pol1         5
3           Pol3         50.00                 3           Pol3         211
5           Pol5         70.00                 5           Pol5         211
7           Pol7         30.00                 7           Pol7         3
8           Pol8         10.00                 8           Pol8         NULL

对于@reasonID = 1

ControlNo   PolicyNumber Premium               ControlNo   PolicyNumber reasonID
----------- ------------ --------------------- ----------- ------------ -----------
3           Pol3         50.00                 3           Pol3         211
5           Pol5         70.00                 5           Pol5         211

对于@reasonID = 2

ControlNo   PolicyNumber Premium               ControlNo   PolicyNumber reasonID
----------- ------------ --------------------- ----------- ------------ -----------
1           Pol1         100.00                1           Pol1         5
7           Pol7         30.00                 7           Pol7         3
8           Pol8         10.00                 8           Pol8         NULL