こんにちは、変数inner join
の各ループで取得されたものを適用して、共通データを抽出する必要があります。最初のループで得られた結果を2番目など@sql
に適用したい。inner join
@sql
たとえば、値を設定しました:
1@3@4@kaal#maal#saal,2@5@6nn#mm
変数で`なので、2回実行されAttributes
ますが、複数回実行される場合があります。得られた各結果は内部結合する必要があり、最終結果は他のテーブルに格納する必要があります。
DECLARE @Attributes NVARCHAR(4000)
SET @Attributes = '1@3@4@kaal#maal#saal,2@5@6nn#mm'
DECLARE @ItemAttributes NVARCHAR(4000)
DECLARE @tblAttributes TABLE (
RowNum INT identity(1, 1)
,Attribute NVARCHAR(1000)
)
INSERT INTO @tblAttributes (Attribute)
SELECT RTRIM(LTRIM(items))
FROM split(@Attributes, ',')
DECLARE @Counter INT
,@RowCount INT
DECLARE @sql NVARCHAR(2000)
SELECT @RowCount = count(RowNum)
FROM @tblAttributes
SET @Counter = 1
WHILE (
@Counter <= @RowCount
OR @Counter = 1
)
BEGIN
SELECT @ItemAttributes = Attribute
FROM @tblAttributes
WHERE RowNum = @Counter
DECLARE @tblAttr TABLE (
RowNum INT identity(1, 1)
,Attr NVARCHAR(1000)
)
INSERT INTO @tblAttr (Attr)
SELECT RTRIM(LTRIM(items))
FROM split(@ItemAttributes, '@')
DECLARE @AttributeID INT
DECLARE @InputTypeID INT
DECLARE @ValidationTypeID INT
DECLARE @AttributeValue NVARCHAR(4000)
SELECT @AttributeID = Attr
FROM @tblAttr
WHERE RowNum = 1
SELECT @InputTypeID = Attr
FROM @tblAttr
WHERE RowNum = 2
SELECT @ValidationTypeID = Attr
FROM @tblAttr
WHERE RowNum = 3
SELECT @AttributeValue = Attr
FROM @tblAttr
WHERE RowNum = 4
IF (@InputTypeID = 1)
BEGIN
IF (@ValidationTypeID = 3)
BEGIN
SELECT @sql = 'SELECT DISTINCT(ItemID) FROM dbo.Aspx_ItemAttributesValue_Decimal
WHERE AttributeValue IN(' + @AttributeValue + ') AND AttributeID =@AttributeID AND (IsDeleted=0 OR IsDeleted IS NULL)'
END
ELSE
IF (@ValidationTypeID = 5)
BEGIN
SELECT @sql = 'SELECT DISTINCT(ItemID) FROM dbo.Aspx_ItemAttributesValue_Int
WHERE AttributeValue IN(' + @AttributeValue + ') AND AttributeID =@AttributeID AND (IsDeleted=0 OR IsDeleted IS NULL)'
END
ELSE
BEGIN
SELECT @sql = 'SELECT DISTINCT(ItemID) FROM dbo.Aspx_ItemAttributesValue_Nvarchar
WHERE AttributeValue IN(' + @AttributeValue + ') AND AttributeID =@AttributeID AND (IsDeleted=0 OR IsDeleted IS NULL)'
END
END
IF (
@InputTypeID = 5
OR @InputTypeID = 6
OR @InputTypeID = 9
OR @InputTypeID = 10
OR @InputTypeID = 11
OR @InputTypeID = 12
)
BEGIN
SELECT @sql = 'SELECT DISTINCT(ItemID) FROM dbo.Aspx_ItemAttributesValue_Options
WHERE AttributeValue IN(' + @AttributeValue + ') AND AttributeID =@AttributeID AND (IsDeleted=0 OR IsDeleted IS NULL)'
END
IF (@InputTypeID = 7)
BEGIN
SELECT @sql = 'SELECT DISTINCT(ItemID) FROM dbo.Aspx_ItemAttributesValue_Decimal
WHERE AttributeValue IN(' + @AttributeValue + ') AND AttributeID =@AttributeID AND (IsDeleted=0 OR IsDeleted IS NULL)'
END
SET @Counter = @Counter + 1
END