sqlserver列转行

it2022-05-05  140

 --字段  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   ';

转载于:https://www.cnblogs.com/YikaJIA/p/8624294.html

相关资源:SqlServer列转行的另一种方式

最新回复(0)