--字段 DECLARE @sql_colH VARCHAR(MAX) SELECT @sql_colH = ISNULL(@sql_colH + ',','') + QUOTENAME([PARAM]) FROM ( --列转行,先转到字段 SELECT * FROM ATE_DATA WHERE RECID=@RECID ) AS A GROUP BY [PARAM] print @sql_colH
SET @sql_table = ' SELECT * FROM ( SELECT [RECID],[STEPID],[PARAM],[VALUE] FROM( select * from [ATE_DATA] a with(nolock) where STEPID!=''H'' and exists(select * from ATE_HEAD_DATA b with(nolock) where a.RECID=b.RECID ) and a.RECID='+@RECID+') AS A) p PIVOT (MAX([VALUE]) FOR [PARAM] IN ('+@sql_colH+') ) AS pvt ';