DECLARE @colsActual AS NVARCHAR(MAX)
DECLARE @colsUpdate AS NVARCHAR(MAX)
DECLARE @colsPlan AS NVARCHAR(MAX)
DECLARE @query AS NVARCHAR(MAX)
select @colsActual = STUFF((SELECT ',' + QUOTENAME(c.Process_Name + '_Actual')
from tb_process c
order by Process_ID
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
select @colsPlan = STUFF((SELECT ',' + QUOTENAME(c.Process_Name + '_Plan')
from tb_process c
order by Process_ID
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
set @query = 'SELECT * FROM
(SELECT * FROM
(SELECT i.Process_ID
, i.ActualDate
, b.Process_Name
FROM tb_report_dwg_center as i
INNER JOIN tb_process as b
ON i.Process_ID = b.Process_ID ) x
PIVOT
(MIN(ActualDate)FOR Process_Name IN (' + @colsActual + ')) pvt
) Dactual
LEFT JOIN
( SELECT * FROM
(SELECT i.Process_ID
, i.PlanDate
, b.Process_Name
FROM tb_report_dwg_center as i
INNER JOIN tb_process as b
ON i.Process_ID = b.Process_ID)
x
PIVOT
(MIN(PlanDate)FOR Process_Name IN (' + @colsPlan + ')) pvt
) Dplan
ON Dactual.Process_ID = Dplan.Process_ID '
exec myStoreProcedure
Error :
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near ')'.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'y'.