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

yogesh kumar rathi 0 Reputation points
2023-10-27T16:09:11.09+00:00

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.
13,688 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 110.4K Reputation points MVP
    2023-10-27T20:46:54.01+00:00

    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. LiHongMSFT-4306 26,791 Reputation points
    2023-10-30T01:53:56.6833333+00:00

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.