sqlserver列转行

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

posted @ 2018-03-22 15:45  勇往直前(yoga)  阅读(4615)  评论(0编辑  收藏  举报