>declare @SQL nvarchar(max), @Columns VARCHAR(max) > >set @columns = stuff(( >select ', ' + quotename(FundType) >FROM ( >SELECT FundType >FROM LookupFundtypes P ) S >WHERE FundType <> '' >ORDER BY FundType >FOR XML PATH('')), 1,2,'') > >print @Columns > >set @SQL = >';with cte as ( >SELECT * >FROM ( >SELECT sponsor, fundtype, amount >from TIP_ApprovedFunding f >inner join TIP_Projects p on p.TipID = f.TipID >where p.ApprovedStatus = [Active] > ) > select * from cte > PIVOT > ( > Sum(amount) > FOR [FundType] > IN (' + @Columns + ') > ) AS PivotTableAlias' > >execute sp_ExecuteSQL @SQL>Note that it attempts to embed a string 'Active' inside the main sql string. This is erroring out with a message of 'incorrect syntax near SELECT'. I am fairly sure the problem is delineating the word 'Active'. Can someone tell me how to do this, or do you see something else wrong with the query?