【问题标题】:My query filtering is not working the way I want it to我的查询过滤没有按我想要的方式工作
【发布时间】:2011-04-18 11:54:56
【问题描述】:

我有 3 种过滤方式:

  1. 按名称
  2. 按列表
  3. 并显示全部

我正在使用 ASP.NET 3.5 和 SQL Server 2008。使用 ADO.NET 和存储过程。

我将列表作为表值参数传递(但我正在使用表变量进行测试)并将名称作为 nvarchar 传递。我将“全部显示”为 ISNULL(@var, column) = column。显然,我查询这个的方式没有利用短路或我对 WHERE 子句如何工作的理解缺乏。发生的情况是,如果我创建 @var = 'some string' 并将 null 插入到表变量中,那么它会正确过滤。如果我使@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

【问题讨论】:

  • 您能否提供一些您正在使用的示例数据?我无法理解您要问的内容。另外,我不相信 SQL Server 使用任何短路:stackoverflow.com/questions/381224/…
  • 您到底想过滤什么?您似乎同时拥有一个变量(脚本中的@resp1)和一个表格。那么,如果@resp1 变量不为空,您希望对它进行过滤,或者如果@resp1 为空,则对表中的所有内容进行过滤?
  • 我正在尝试过滤 @resp1 或 @userTable。因此,要么按一个名称过滤,要么按名称列表过滤。
  • @Andrew:是的,这就是我想要做的。它似乎没有工作。

标签: sql sql-server filtering table-valued-parameters


【解决方案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)

然后看看是否返回了想要的结果。

【讨论】:

  • 不幸的是它没有。但我不在比较空值的地方。 ISNULL 返回第一个非空值是否正确?
【解决方案2】:

您是否考虑过将 WHERE 子句更改为:

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

而不是在表值参数上加入?

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    相关资源
    最近更新 更多