So scheint es zu funktionieren. Jedoch muss man, IMO, bei PIVOT ein Aggregat angeben.
Code:
DECLARE @cols AS NVARCHAR(MAX),
@
query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(FIELD_NAME)
from Table1
FOR
XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @
query = 'SELECT ' + @cols + '
from
(
select FIELD_NAME, ID
from Table1
) x
pivot
(
max(ID)
for FIELD_NAME in (' + @cols + ')
) p '
execute(@
query)