我有3种方法可以过滤:
我正在使用ASP.NET 3.5和SQL Server 2008.使用ADO.NET和存储过程。
我将我的列表作为表值参数传递(但我正在使用表变量进行测试)并将名称作为nvarchar传递。我有“显示所有”作为ISNULL(@ var,column)=列。显然,我查询的方式并不是利用短路或我对WHERE子句如何工作的理解缺乏。发生的事情是,如果我将@var ='some string'并将null插入表变量,那么它会正确过滤。如果我将@var = null并将'some string'插入表变量,那么我得到每条记录,我应该得到'some string'。
代码:
declare @resp1 nvarchar(32)
set @resp1 = null
declare @usersTable table
(responsible nvarchar(32))
--insert into @usersTable (responsible) values (null)
insert into @usersTable (responsible) values ('ssimpson')
insert into @usersTable (responsible) values ('kwilcox')
select uT.responsible, jsq.jobnumber, jsq.qid, aq.question, aq.section, aq.seq, answers.*
from answers
inner join jobno_specific_questions as jsq on answers.jqid = jsq.jqid
inner join apqp_questions as aq on jsq.qid = aq.qid
left join @usersTable as uT on uT.responsible = answers.responsible
where answers.taskAction = 1 and (uT.responsible is not null or ISNULL(@resp1, Answers.responsible) = Answers.responsible)
order by aq.section, jsq.jobnumber, answers.priority, aq.seq
这就是我想出来的。虽然很难看......
declare @resp1 nvarchar(32)
set @resp1 = 'rrox'
declare @filterPick int
declare @usersTable table
(responsible nvarchar(32))
insert into @usersTable (responsible) values (null)
--insert into @usersTable (responsible) values ('ssimpson')
--insert into @usersTable (responsible) values ('kwilcox')
if @resp1 is null
begin
set @filterPick = 2
end
else
begin
set @filterPick = 1
end
select uT.responsible, jsq.jobnumber, jsq.qid, aq.question, aq.section, aq.seq, answers.*
from answers
inner join jobno_specific_questions as jsq on answers.jqid = jsq.jqid
inner join apqp_questions as aq on jsq.qid = aq.qid
left join @usersTable as uT on uT.responsible = answers.responsible
where answers.taskAction = 1 and
(case
when uT.responsible is not null then 2
when ISNULL(@resp1, Answers.responsible) = Answers.responsible then 1
end = @filterPick )
order by aq.section, jsq.jobnumber, answers.priority, aq.seq
确定。我想我已经明白了。我删除了@resp1,因为它没有必要,我只是使用表值参数@usersTable(但在这里我使用表变量进行测试)。我添加了一个标志@filterPick,所以我只能在@usersTable中显示值,或者每个记录都显示answers.taskAction = 1。
代码:
declare @filterPick bit
declare @usersTable table
(responsible nvarchar(32))
insert into @usersTable (responsible) values (null)
--insert into @usersTable (responsible) values ('ssimpson')
--insert into @usersTable (responsible) values ('kwilcox')
if exists (select * from @usersTable where responsible is not null)
begin
set @filterPick = 1
end
else
begin
set @filterPick = 0
end
select *
from answers
inner join jobno_specific_questions as jsq on answers.jqid = jsq.jqid
inner join apqp_questions as aq on jsq.qid = aq.qid
left join @usersTable as uT on answers.responsible = uT.responsible
where answers.taskAction = 1 and (uT.responsible is not null or (isnull(uT.responsible, answers.responsible) = answers.responsible and @filterPick = 0))
order by aq.section, jsq.jobnumber, answers.priority, aq.seq
答案 0 :(得分:1)
我对你的问题感到有些困惑,但我会试一试。
首先,我怀疑您返回错误记录的问题与您的空值比较有关。为了演示我在说什么,查询你想要的任何表,并将其添加到结尾:
WHERE null = null
不会返回任何记录。在你的代码中我会改变:
where answers.taskAction = 1 and (uT.responsible is not null or ISNULL(@resp1, Answers.responsible) = Answers.responsible)
到
where answers.taskAction = 1 and (uT.responsible is not null or @resp1 is null)
看看是否会返回所需的结果。
答案 1 :(得分:0)
您是否考虑过将WHERE子句更改为:
WHERE Answers.taskAction = 1
AND(ISNULL(@resp1, Answers.responsible) = Answers.responsible
OR Answers.responsible IN (SELECT responsible FROM uT))
而不是在表值参数上加入?