1

こんにちは、変数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
4

1 に答える 1

1

これを試してみてください -

DECLARE 
      @Attributes NVARCHAR(2000)
    , @SQL NVARCHAR(MAX)

SELECT @Attributes = '1@3@4@kaal#maal#saal,2@5@6@nn#mm'

DECLARE @tblAttributes TABLE 
(
      RowNum INT IDENTITY(1, 1)
    , Attribute NVARCHAR(1000)
)

INSERT INTO @tblAttributes (Attribute)
SELECT RTRIM(LTRIM(p.value('.', 'NVARCHAR(1000)')))
FROM (
    SELECT field = CAST('<s>' + REPLACE(@Attributes, ',', '</s><s>') + '</s>' AS XML)  
) d
CROSS APPLY field.nodes('/s') t(p)

DECLARE 
      @Counter INT
    , @RowCount INT

SELECT 
      @RowCount = COUNT(RowNum)
    , @Counter = 1
FROM @tblAttributes

WHILE @Counter <= @RowCount OR @Counter = 1 BEGIN

    DECLARE 
          @AttributeID INT
        , @InputTypeID INT
        , @ValidationTypeID INT
        , @AttributeValue NVARCHAR(4000)

    SELECT 
          @AttributeID = [1]
        , @InputTypeID = [2]
        , @ValidationTypeID = [3]
        , @AttributeValue = [4]
    FROM (
        SELECT 
              Attribute = RTRIM(LTRIM(p.value('.', 'NVARCHAR(4000)')))
            , rn = ROW_NUMBER() OVER (ORDER BY (SELECT 1))
        FROM (
            SELECT field = CAST('<s>' + REPLACE(Attribute, '@', '</s><s>') + '</s>' AS XML)  
            FROM @tblAttributes
            WHERE RowNum = @Counter
        ) d
        CROSS APPLY field.nodes('/s') t(p)
    ) r
    PIVOT
    (
        MAX(Attribute)
        FOR rn IN ([1], [2], [3], [4])
    )p

    SELECT @SQL = 
    CASE 
        WHEN @InputTypeID = 1 
            THEN 
            CASE 
                WHEN @ValidationTypeID = 3 
                    THEN 'SELECT DISTINCT(ItemID) 
                          FROM dbo.Aspx_ItemAttributesValue_Decimal
                          WHERE AttributeValue IN (' + @AttributeValue + ') 
                              AND AttributeID =@AttributeID AND ISNULL(IsDeleted, 0) = 0'
                WHEN @ValidationTypeID = 5
                    THEN 'SELECT DISTINCT(ItemID) 
                          FROM dbo.Aspx_ItemAttributesValue_Int
                          WHERE AttributeValue IN (' + @AttributeValue + ') 
                              AND AttributeID =@AttributeID AND ISNULL(IsDeleted, 0) = 0'
                ELSE 'SELECT DISTINCT(ItemID) 
                          FROM dbo.Aspx_ItemAttributesValue_Nvarchar
                          WHERE AttributeValue IN (' + @AttributeValue + ') 
                              AND AttributeID =@AttributeID AND ISNULL(IsDeleted, 0) = 0'
            END
        WHEN @InputTypeID IN (5, 6, 9, 10, 11, 12) 
            THEN 'SELECT DISTINCT(ItemID) 
                  FROM dbo.Aspx_ItemAttributesValue_Options 
                  WHERE AttributeValue IN (' + @AttributeValue + ') 
                      AND AttributeID = @AttributeID AND ISNULL(IsDeleted, 0) = 0'
        WHEN @InputTypeID = 7 
            THEN 'SELECT DISTINCT(ItemID) 
                  FROM dbo.Aspx_ItemAttributesValue_Decimal 
                  WHERE AttributeValue IN (' + @AttributeValue + ') 
                      AND AttributeID = @AttributeID AND ISNULL(IsDeleted, 0) = 0'
    END

    SET @Counter = @Counter + 1

END

SELECT @SQL
于 2013-06-03T08:57:18.227 に答える