2010-09-28 85 views
3

的方式,我有3个方面,我想过滤:我查询过滤不工作,我希望它

按名称
  1. 通过列表
  2. 并显示所有

我使用ASP.NET 3.5和SQL Server 2008.使用ADO.NET和存储特效。

我传递我的列表作为表值参数(但我正在测试一个表变量)和名称作为nvarchar。我有“全部显示”为ISNULL(@var,column)=列。很明显,我查询的方式没有利用短路或我对WHERE子句缺乏工作的理解。发生什么事情是,如果我使@var ='一些字符串'并向表变量插入一个空值,那么它会正确过滤。如果我使@var = null并向表变量中插入'一些字符串',那么我得到每一条记录,我应该得到'一些字符串'。

代码:

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

您能提供一些您正在使用的示例数据吗?我无法理解你想问什么。此外,我不相信SQL Server使用任何短路:http://stackoverflow.com/questions/381224/sql-server-query-short-circuiting – 2010-09-28 19:39:35

+0

什么是你想过滤?您似乎同时拥有一个变量(脚本中的@ resp1)和一个表格。所以,如果@ resp1变量不是null或者表中的所有内容都是过滤器,那么你需要@ resp1变量的过滤器,如果@ resp1为null? – Andrew 2010-09-28 19:39:49

+0

我试图过滤@ resp1或@userTable。所以要么过滤一个名字,要么过一个名字列表。 – dotnetN00b 2010-09-28 19:51:33

回答

0

你有没有考虑改变WHERE子句的东西如:

WHERE Answers.taskAction = 1 
AND(ISNULL(@resp1, Answers.responsible) = Answers.responsible 
    OR Answers.responsible IN (SELECT responsible FROM uT)) 

而不是加入表值参数?

+0

试过了。与JOIN具有相同的效果。但是,谢谢。 – dotnetN00b 2010-09-29 13:11:13

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) 

,看看是否能返回所需的结果。

+0

不幸的是它没有。但我不会在哪里比较空值。 ISNULL返回第一个非空值是否正确? – dotnetN00b 2010-09-29 13:10:45