连接动态SQL列

时间:2011-06-14 18:43:12

标签: tsql dynamic concatenation

我有三张桌子; tblProducts和tblOption以及tblOptionValue。 (T-SQL)

我使用此查询获得了所有可能的产品组合:

DECLARE @ProductId INT, @Query NVARCHAR(MAX), @ProductOptionGroupId INT
SET @ProductId = 69
SET @Query = ''

DECLARE CC CURSOR FOR
SELECT DISTINCT OptionID
FROM tblOption
WHERE ProductId = @ProductId

OPEN CC
FETCH NEXT FROM CC INTO @ProductOptionGroupId
WHILE @@FETCH_STATUS = 0
BEGIN
    SET @Query = @Query + '(SELECT DISTINCT tblOptionValue.Name FROM tblOptionValue INNER JOIN tblOption on tblOptionValue.OptionID = tblOption.OptionID WHERE tblOptionValue.OptionId='+CAST(@ProductOptionGroupId AS VARCHAR)+'AND tblOption.ProductId='+CAST(@ProductId AS VARCHAR)+
                 +') AS Table' + CAST(@ProductOptionGroupId AS VARCHAR)+' CROSS JOIN '
    FETCH NEXT FROM CC INTO @ProductOptionGroupId
END
CLOSE CC
DEALLOCATE CC
SET @Query = 'SELECT * FROM ' + LEFT(@Query,LEN(@Query)-10) 
PRINT @Query
EXEC sp_executesql @Query

感谢@Lamak here 我已将其改编为我的数据库,但我需要:

A)找到一种方法将所有数据拉回到一列而不是多列中 B)将tblOptionValue.Name列命名为与关联的tblOption.Name字段相同

关于如何实现上述目标的任何想法?

非常感谢

更新:我使用以下查询设法实现了目标:

DECLARE @ProductId INT, @Query NVARCHAR(MAX), @ProductOptionGroupId INT, @cName     VARCHAR(300)
SET @ProductId = 70
SET @Query = ''

DECLARE CC CURSOR FOR
SELECT DISTINCT OptionID
FROM tblOption
WHERE ProductId = @ProductId

OPEN CC
FETCH NEXT FROM CC INTO @ProductOptionGroupId
WHILE @@FETCH_STATUS = 0
BEGIN
    set @cName = (SELECT Name FROM tblOption WHERE OptionID=@ProductOptionGroupId)
    SET @Query = @Query + '(SELECT DISTINCT tblOptionValue.Name as '+@cName+' FROM tblOptionValue INNER JOIN tblOption on tblOptionValue.OptionID = tblOption.OptionID INNER JOIN tblProduct on tblOption.ProductID=tblProduct.ProductID WHERE tblOptionValue.OptionId='+CAST(@ProductOptionGroupId AS VARCHAR)+'AND tblOption.ProductId='+CAST(@ProductId AS VARCHAR)+
                 +') AS Table' + CAST(@ProductOptionGroupId AS VARCHAR)+' CROSS JOIN '    
    FETCH NEXT FROM CC INTO @ProductOptionGroupId
END
CLOSE CC
DEALLOCATE CC
SET @Query = 'SELECT t2.*, t1.* FROM (SELECT * FROM ' + LEFT(@Query,LEN(@Query)-10)
SET @Query = @Query + ') as t1, 
(SELECT * FROM tblProduct WHERE ProductID=70) as t2'

PRINT @Query
EXEC sp_executesql @Query

1 个答案:

答案 0 :(得分:2)

将用户的解决方案放入答案中。

DECLARE @ProductId INT, @Query NVARCHAR(MAX), @ProductOptionGroupId INT, @cName     VARCHAR(300)
SET @ProductId = 70
SET @Query = ''

DECLARE CC CURSOR FOR
SELECT DISTINCT OptionID
FROM tblOption
WHERE ProductId = @ProductId

OPEN CC
FETCH NEXT FROM CC INTO @ProductOptionGroupId
WHILE @@FETCH_STATUS = 0
BEGIN
    set @cName = (SELECT Name FROM tblOption WHERE OptionID=@ProductOptionGroupId)
    SET @Query = @Query + '(SELECT DISTINCT tblOptionValue.Name as '+@cName+' FROM tblOptionValue INNER JOIN tblOption on tblOptionValue.OptionID = tblOption.OptionID INNER JOIN tblProduct on tblOption.ProductID=tblProduct.ProductID WHERE tblOptionValue.OptionId='+CAST(@ProductOptionGroupId AS VARCHAR)+'AND tblOption.ProductId='+CAST(@ProductId AS VARCHAR)+
                 +') AS Table' + CAST(@ProductOptionGroupId AS VARCHAR)+' CROSS JOIN '    
    FETCH NEXT FROM CC INTO @ProductOptionGroupId
END
CLOSE CC
DEALLOCATE CC
SET @Query = 'SELECT t2.*, t1.* FROM (SELECT * FROM ' + LEFT(@Query,LEN(@Query)-10)
SET @Query = @Query + ') as t1, 
(SELECT * FROM tblProduct WHERE ProductID=70) as t2'

PRINT @Query
EXEC sp_executesql @Query