多个动态选择语句的内部联接

时间:2013-06-03 07:14:20

标签: sql sql-server sql-server-2008 tsql sql-server-2005

您好我需要通过应用在inner join变量中的每个循环上获得的@sql来提取公共数据。我想对inner join上的第一个循环获得的结果应用@sql到第二个等等。

例如我设置了值:

1@3@4@kaal#maal#saal,2@5@6nn#mm

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

1 个答案:

答案 0 :(得分: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