How to insert the rows into #temp table for a pivoted results

yogesh kumar rathi 0 Reputation points

How to insert the rows into #temp table for a pivoted results

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
11,585 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 92,961 Reputation points

    Exactly in the same way as you would insert any other result set.

    If your comment is "my result set is dynamic", the answer is that you don't. A dynamic pivot is a non-relational operation, and the only useful thing you can do with a dynamic pivot is return the data to the client.

    0 comments No comments

  2. CosmogHong-MSFT 17,156 Reputation points Microsoft Vendor

    Hi @yogesh kumar rathi

    Please check this sample:

    declare @source_table table (id int,[type] varchar(20), val int)
    insert into @source_table values(1,'AA',98),(2,'AA',56),(3,'BB',26),(4,'CC',47)
    select * into #temp
    from @source_table pivot(max(val) for [type] in ([AA],[BB],[CC]))p
    select * from #temp

    Best regards,

    Cosmog Hong

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments