Search code examples
sqlsql-serverstringt-sqltable-variable

Convert SQL code (as string) into table - dynamic pivot


I have used the below SQL script to perform dynamic pivot. I want to use the result to left join on another table, but the "INTO" method does not seem to work for the table variable @query. It shows an error:

Must declare the table variable "@query"

If I directly join the table like this: LEFT JOIN @query. How should I save @query in order to left join on another table? Thanks!

create table temp
(
    date datetime,
    category varchar(3),
    amount money
)

insert into temp values ('1/1/2012', 'ABC', 1000.00)
insert into temp values ('2/1/2012', 'DEF', 500.00)
insert into temp values ('2/1/2012', 'GHI', 800.00)
insert into temp values ('2/10/2012', 'DEF', 700.00)
insert into temp values ('3/1/2012', 'ABC', 1100.00)


DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.category) 
            FROM temp c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT date, ' + @cols + ' from 
            (
                select date
                    , amount
                    , category
                from temp
           ) x
            pivot 
            (
                 max(amount)
                for category in (' + @cols + ')
            ) p '


execute(@query)

drop table temp

Solution

  • I got it figured out. The solution is to add ##result in the string sql script as below:

    set @query = 'SELECT date, ' + @cols + ' ##result from 
                (
                    select date
                        , amount
                        , category
                    from temp
               ) x
                pivot 
                (
                     max(amount)
                    for category in (' + @cols + ')
                ) p '
    
    select * from ##result