的方式,我有3個方面,我想過濾:我查詢過濾不工作,我希望它
按名稱- 通過列表
- 並顯示所有
我使用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
您能提供一些您正在使用的示例數據嗎?我無法理解你想問什麼。此外,我不相信SQL Server使用任何短路:http://stackoverflow.com/questions/381224/sql-server-query-short-circuiting – 2010-09-28 19:39:35
什麼是你想過濾?您似乎同時擁有一個變量(腳本中的@ resp1)和一個表格。所以,如果@ resp1變量不是null或者表中的所有內容都是過濾器,那麼你需要@ resp1變量的過濾器,如果@ resp1爲null? – Andrew 2010-09-28 19:39:49
我試圖過濾@ resp1或@userTable。所以要麼過濾一個名字,要麼過一個名字列表。 – dotnetN00b 2010-09-28 19:51:33