其实,你的结果是
select * from something where ps.Category ='some string, containing and ps.Status= inside'
所以空行集预期的结果。
你想是这样的(希望状态数量,而不是字符串)
select * from Projects p
join projectstatus ps on p.pid = ps.pid
where ps.Category = isnull(@Category, p.Category) and
(@Status is NULL OR ps.Status = @Status)
好,这里是不信任的测试:-)
declare @projects table
(
pid int,
name nvarchar(20),
category int
);
declare @projectstatus table
(
pid int,
Category int,
status int
);
insert into @projects values
(1,'Project 1', 1),(2,'Project 2',1),(3,'Project 3',1),(4,'Project 4',1),(5,'Project 5',1);
insert into @projectstatus values
(1,1,1),(2,1,2),(3,1,3),(4,1,2),(5,1,NULL);
declare @Category int =null;
declare @Status int;
--first of all, do not understand, what is the logic with category
--category in one table should be the same, than in other table or specified?
--ok, you said with category everything is ok, do not test category, test status
--test with null
set @Status=null
select * from @Projects p
join @projectstatus ps on p.pid = ps.pid
where ps.Category = isnull(@Category, p.Category) and
(@Status is NULL OR ps.Status = @Status)
--test with existing status
set @Status=1
select * from @Projects p
join @projectstatus ps on p.pid = ps.pid
where ps.Category = isnull(@Category, p.Category) and
(@Status is NULL OR ps.Status = @Status)
--test with not existing status
set @Status=10
select * from @Projects p
join @projectstatus ps on p.pid = ps.pid
where ps.Category = isnull(@Category, p.Category) and
(@Status is NULL OR ps.Status = @Status)
(@Status为空或PS .Status = @Status)总是会返回完整列表。它永远不会过滤。 –
不同意。例如@Status = 1,将返回状态为1的行,并且不显示状态为2的行。我甚至可以给出临时表 – vitalygolub
的示例我诚挚的道歉。我误读了第一部分作为ps.status –