3

フィルタリングする方法は 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

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
4

2 に答える 2

1

私はあなたの質問に少し混乱していますが、試してみます。

まず、間違ったレコードが返されるという問題は、null 値の比較に関係していると思われます。私が話していることを示すために、必要なテーブルをクエリして、これを最後に追加します。

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)

そして、それが望ましい結果を返すかどうかを確認してください。

于 2010-09-28T19:44:02.353 に答える
0

WHERE 句を次のように変更することを検討しましたか。

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

テーブル値パラメーターで JOIN する代わりに?

于 2010-09-28T19:41:35.887 に答える