I'm getting:Msg 102, Level 15, State 1, Line 8Incorrect syntax near '('.If highlight the first 8 rows and run the query it completes successfully. If I highlight the remaining it completes. When I run the entire thing together I get the message. Running SQL 2008DECLARE @QUERY VARCHAR(4000)DECLARE @years VARCHAR(2000)SELECT @years = STUFF(( SELECT DISTINCT '],[' + (str(year((oehdrhst_sql.inv_dt)))) FROM oehdrhst_sql ORDER BY '],[' + (str(YEAR(oehdrhst_sql.inv_dt))) FOR XML PATH('') ), 1, 2, '') + ']' SET @query ='SELECT * FROM(SELECT OEHDRHST_SQL.cus_no,YEAR(OEHDRHST_SQL.inv_dt),OEHDRHST_SQL.tot_sls_amt FROM oehdrhst_sql )tPIVOT (SUM(OEHDRHST_SQL.tot_sls_amt) FOR year(oehdrhst_sql.inv_dt)IN ('+@years+'))) AS pvt' EXECUTE (@query)