DECLARE @cmd NVARCHAR(MAX);
DECLARE @names NVARCHAR(MAX);
SET @names = STUFF(( SELECT ',' + Name
FROM dbo.ProposedProjectSettings
WHERE ProposedProjectID = '100012'
FOR
XML PATH('')
), 1, 1, '');
SET @cmd = '
SELECT *
FROM ( SELECT Name ,
Value
FROM dbo.ProposedProjectSettings
WHERE ProposedProjectID = ''100012''
) p PIVOT( MIN(Value) FOR Name IN ( '+@names+' ) )
AS pvt;';
EXEC(@cmd)
转置前
转置后