I am pivoting a table based on https://stackoverflow.com/a/10404455/1349141
This code works perfect. However, when I modify the code for my tables. It shows a mysterious Incorrect syntax near '.' for the row below the second declare
.
Here is my code
DECLARE @cols AS NVARCHAR(MAX)=''
DECLARE @query AS NVARCHAR(MAX)=''
--Incorrect syntax near '.'
SELECT @cols = @cols + QUOTENAME(Crop) + ',' FROM (select distinct [Crop] from [FAF40].[dbo].[county_farm_acres2014] ) as tmp
set @query =
'SELECT * from
(
select StateCode, CountyCode, Crop from [FAF40].[dbo].[county_farm_acres2014]
) src
pivot
(
max(PlantedAcres) for Crop in (' + @cols + ')
) piv'
execute(@query)
go