Sql 我的查询筛选没有按我希望的方式工作
我有3种过滤方法:Sql 我的查询筛选没有按我希望的方式工作,sql,sql-server,filtering,table-valued-parameters,Sql,Sql Server,Filtering,Table Valued Parameters,我有3种过滤方法: 名字 按列表 向大家展示 我正在使用ASP.NET3.5和SQLServer2008。使用ADO.NET和存储过程 我将列表作为表值参数传递(但我使用表变量进行测试),并将名称作为nvarchar传递。我将“show all”设置为ISNULL(@var,column)=column。显然,我质疑这一点的方式不是利用短路,也不是我对子句如何工作的理解不足。所发生的事情是,如果我将@var='somestring'设置为空,并在表变量中插入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 @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
这就是我想到的。虽然很难看
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 @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 @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
您是否考虑过将WHERE子句更改为类似以下内容:
WHERE Answers.taskAction = 1
AND(ISNULL(@resp1, Answers.responsible) = Answers.responsible
OR Answers.responsible IN (SELECT responsible FROM uT))
不是加入表值参数?我对你的问题有点困惑,但我将试一试 首先,我怀疑返回错误记录的问题与比较空值有关。要演示我所说的内容,请查询您想要的任何表并将其添加到末尾:
WHERE null = null
不会返回任何记录。在您的代码中,我将更改:
where answers.taskAction = 1 and (uT.responsible is not null or ISNULL(@resp1, Answers.responsible) = Answers.responsible)
到
然后查看是否返回所需的结果。您能提供一些您正在处理的示例数据吗?我很难理解你想问什么。另外,我不相信SQLServer使用任何短路:您到底想过滤什么?您似乎有一个变量(脚本中有@resp1)和一个表。因此,如果@resp1变量不为null,您希望对其进行筛选;如果@resp1为null,您希望对表中的所有内容进行筛选?我正在尝试对@resp1或@userTable进行筛选。所以要么按一个名字过滤,要么按一组名字过滤。@安德鲁:是的,这就是我想做的。它似乎不起作用。不幸的是它不起作用。但我不知道我在哪里比较空值。ISNULL返回第一个非null值是否正确?