Sql server 当在SQL语句中将NULL作为参数传递时,如何调整WHERE子句以选择所有记录而不使用Premium=0
结果应返回:Sql server 当在SQL语句中将NULL作为参数传递时,如何调整WHERE子句以选择所有记录而不使用Premium=0,sql-server,tsql,sql-server-2012,Sql Server,Tsql,Sql Server 2012,结果应返回: 如果@reasonID=1我只需要选择具有reasonID=211 如果@reasonID=2我只需要选择具有reasonID 211的策略,包括reasonID为NULL 如果@reasonID=NULL我需要选择所有保单,包括NULL和Premium 0 下面的示例适用于@reasonID=1和@reasonID=2: ControlNo PolicyNumber Premium ControlNo PolicyNumber reason
- 如果
我只需要选择具有@reasonID=1
reasonID=211
- 如果
我只需要选择具有@reasonID=2
的策略,包括reasonID 211
reasonID为NULL
- 如果
我需要选择所有保单,包括@reasonID=NULL
和NULL
Premium 0
@reasonID=1
和@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
@reasonID=1
@reasonID=2
但是当@reasonID=NULL
时,如何调整WHERE
子句来选择所有行呢?因为它返回的策略具有Premium=0
,我不需要它
@reasonID=NULL
declare@tentalble1表(ControlNo int,policynumbervarchar(50),Premium money)
插入@1
数值(1,'Pol1',100),(2,'Pol2',0),(3,'Pol3',50),(4,'Pol4',0),
(5,'Pol5',70),(6,'Pol6',0),(7,'Pol7',30)
declare@tentable2表(ControlNo int,PolicyNumber varchar(50),reasonID int)
插入@2
值(1,'Pol1',5),(2,'Pol2',NULL),(3,'Pol3',211),
(4,'Pol4',8),(5,'Pol5',211),(6,'Pol6',空),
(7,'Pol7',3)
--从@testable1中选择*
--从@testable2中选择*
--这里我输入@reasonID参数
声明@reasonID int=NULL
选择
T2.ControlNo,T2.PolicyNumber,T1.Premium,T2.reasonID
从…起
@诱人的1 T1
内连接
@t1.ControlNo=T2.ControlNo上的诱惑2 T2
哪里
T1.保费0
和(当reasonID=211时,则为1 else 2 end=@reasonID)--适用于@reasonID=1或@reasonID=2
或(@reasonID为NULL)--不起作用
但应该是这样的:
是否有任何方法可以修改
WHERE
子句以获得理想的结果,而无需使用have
子句或GROUP BY
?我想您可能没有添加一个括号。另外,我在where条件中修改了case语句,因为else条件也会考虑null值。既然你有空值的or条件,现在就不重要了。我假设您可能不想在case语句中使用null条件,所以我对其进行了更改。我在您的输入中没有看到策略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)
我相信你需要这样的东西:
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))
)
对于
@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
函数
coalesce
是您在这里的朋友。也可以考虑< <代码> > < <代码>表达式。谢谢,但是我也尝试如果参数=2,也可以为真值行设置空值。因此,它适用于@reasonID=1
和@reasonID
=NULL,但不适用于@reasonID
=2@Oleg您想要无条件(@reasonID为NULL)?当@reasonID
=NULL时,我希望它选择任何“reasonID”(包括NULL),但高级
应为0。此选项同样有效:和((tblQuotes.QUOTESTATUSREANOTID=211时为1,QUOTESTATUSREANOTID 211或QUOTESTATUSREANOTID为NULL时为1,则为2 END=@isExonorated)或(@isExonorated为NULL))
ControlNo PolicyNumber Premium ControlNo PolicyNumber reasonID
----------- ------------ --------------------- ----------- ------------ -----------
3 Pol3 50.00 3 Pol3 211
5 Pol5 70.00 5 Pol5 211
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