I have tried all below links ,But they are not working in my Sever SQL?
SET @cols = STUFF((SELECT ',' + QUOTENAME(AD.MonthFormat) FROM #tempMonthFormat AD FOR xml PATH (''), TYPE).value('.', 'nvarchar(max)'), 1, 1, '')
SELECT @Selectcols = STUFF((SELECT
',' + ISNULL(@Selectcols + ',','')+ 'ISNULL(' + QUOTENAME(AD.MonthFormat) + ', 0) AS ' + QUOTENAME(AD.MonthFormat) FROM #tempMonthFormat AD
FOR xml PATH (''), TYPE)
.value('.', 'nvarchar(max)'), 1, 1, '');
Declare @FINALQUERY varchar(max);
SET @FINALQUERY = '
SELECT Name,[Issue],' + @Selectcols + ' FROM
(
SELECT into #temptable * FROM #Meantimeresult
) Y
PIVOT
(
SUM(MT)
FOR [Monthformat] IN (' + @cols + ')
) p GROUP BY ' + @cols+',[Issue], Name'
EXEC SP_EXECUTESQL @FINALQUERY;
Dynamic SQL Result INTO #Temp Table
How to create temp table from a dynamic query?
INSERT INTO temp table from unknown number and name of columns (from dynamic PIVOT query) Dynamic Pivot Results to a Temp Table