--字段 DECLARE @sql_colH VARCHAR(MAX) SELECT @sql_colH = ISNULL(@sql_colH + ‘,‘,‘‘) + QUOTENAME([PARAM]) FROM ( --列转行,先转到字段 SELECT * FROM ATE_DATA WHERE [email protected] ) 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=‘[email protected]+‘) AS A) p PIVOT (MAX([VALUE]) FOR [PARAM] IN (‘[email protected]_colH+‘) ) AS pvt ‘;