In this code I am converting the subjects(columns) English , Hindi , Sanskrith , into rows
DECLARE @colsUnpivot AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @colsUnpivot
=stuff(
(select ',' + quotename (C.name)
from sys.columns c
where c.object_id = OBJECT_ID('dbo.result2')
for xml path(''), TYPE) .value('.', 'NVARCHAR(MAX)'),
1, 8, ''
);
print @colsunpivot
select @query
= 'select Name, Subject,Marks
from result2
unpivot
(
marks for subject in (' + @colsunpivot + ')) as tab'
exec sp_executesql @query;
Run Code Online (Sandbox Code Playgroud)
Please explain what does for xml …