DECLARE @query VARCHAR(max)
DECLARE @years VARCHAR(max)
SELECT @years = STUFF(( SELECT DISTINCT '],[' + ltrim(str(Years)) FROM
#Inquires ORDER BY '],[' + ltrim(str(Years)) desc FOR XML
PATH('')), 1, 2, '') + ']'
SET @query = 'INSERT INTO Table_Name SELECT *
FROM (SELECT ReportDate, Company, EventType, Years,Months#,
Months,Inquires FROM #Inquires
WHERE Company = ''Company_Name'')t PIVOT (SUM(Inquires)
FOR Years IN ('+@years+')) AS pvt ORDER BY Company, Months#'
EXECUTE (@query)
I am getting this error while executing this code:
Column name or number of supplied values does not match table definition
This code is running fine in SQL Server 2008, but not in SQL Server 2014.
I'm guessing that you now have more years returned from your pivot
than you have in your table.
Try specifying your column names for your insert
and in your select
.