Storing dynamic Pivot result into a temporary table in SQL Server
问题 I have the following schema and sample data. create table MyTable ( Id int, Year int, Channel varchar(10), Payments int ) insert into MyTable values (1,2012,'HV',100), (1,2014,'HV',56), (2,2012,'NL',17000) (2,2012,'HV',495), (3,2013,'HV',565) Now I want to create and insert dynamic pivot data in a temporary table. I am able to create the pivot data as the demo here. But I want to store this data into a temporary table. What I have tried is as below. Declare @SQL varchar(max) = ' if object_id(