私が達成しようとしていることを説明します:
テーブル変数に入れた名前と値のペアを含むxmlをSPに渡しています@nameValuePairs
。名前と値のペア (属性、別のテーブル) が完全に一致する式 (テーブル) の ID のリストを取得する必要があります。
これは私のスキーマです:
式テーブル--> (expressionId、attributeId)
属性テーブル--> (attributeId、attributeName、attributeValue)
動的 SQL と邪悪なカーソル (これは動作しますが、非常に遅い) で複雑なことを試した後、これが私が今持っているものです:
--do the magic plz!
-- retrieve number of name-value pairs
SET @noOfAttributes = select count(*) from @nameValuePairs
select distinct
e.expressionId, a.attributeName, a.attributeValue
into
#temp
from
expressions e
join
attributes a
on
e.attributeId = a.attributeId
join --> this join does the filtering
@nameValuePairs nvp
on
a.attributeName = nvp.name and a.attributeValue = nvp.value
group by
e.expressionId, a.attributeName, a.attributeValue
-- now select the IDs I need
-- since I did a select distinct above if the number of matches
-- for a given ID is the same as noOfAttributes then BINGO!
select distinct
expressionId
from
#temp
group by expressionId
having count(*) = @noOfAttributes
問題を見つけられるかどうかを確認してください。これを行うより良い方法はありますか?
どんな助けでも大歓迎です!