开发者

Concatenate Dynamic SQL Columns

I've got three tables; tblProducts and tblOption and tblOptionValue. (T-SQL)

I get all开发者_如何学运维 possible combinations of my product using the this query:

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

Found here thanks to @Lamak I've adapted it to my database but I need to:

A) Find a way to pull all the data back in one column instead of multiple columns

B) Name the tblOptionValue.Name column the same as the associated tblOption.Name field

Any thoughts on how I might achieve the above?

Many thanks

Update: I managed to achieve my goals, using the following query:

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


Put the user's solution into an answer.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜