フィルタリングする方法は 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
Ok。私はそれを手に入れたと思います。@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