Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
DECLARE @cols NVARCHAR(300)
DECLARE @dt NVARCHAR(1000)
DECLARE @pt NVARCHAR(2000)

SET @cols = STUFF((SELECT DISTINCT '],[' + [text]
                    FROM B
                    ORDER BY '],[' + [text]
            FOR XML PATH('')),1,2,'') + ']'


SET @dt = 'SELECT B.[ID], B.[Text], B.[Value] ' +
        'FROM A LEFT JOIN B ON A.[ID] = B.[ID]  '
EXEC(@dt)

SET @pt = 'SELECT [ID], ' + @cols + ' ' +
        'FROM (' + @dt + ') AS DT ' +
        'PIVOT(MAX([Value]) FOR [Text] IN (' + @cols + ')) AS PT ' +
        'ORDER BY [ID]  '
EXEC(@pt)

I have this stored procedure in which I create dynamic column now I want to store that result in temp table.
Posted
Updated 18-Nov-12 22:32pm
v2

For creating Temporary table :
EX :
create table #tempTable(Name varchar(30)) // #TableName for creating temp table
 
Share this answer
 
For create temporary table:

SQL
Create Table #tablename(columnname datatype,...)


Every thing are almost same as normal table create command except table name. A # symbol add as prefix.

The table available within the scope. And it's better to drop table by
SQL
drop table #tablename
after use
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900