Sql 行转列 STUFF



  SELECT  DISTINCT BizID
  ,STUFF(
  (
    SELECT ',' + ApproveName
    FROM T_Common_ApproveNode
            
    WHERE BizID = A.BizID
    FOR XML PATH('')
  )
  ,1
  ,1
  ,''
 ) AS ApproveNameStr
FROM T_Common_ApproveNode AS A

BizID                           | ApproveNameStr
000835d70a99447f96b21d4b827c45d8|何磊,胡庄,陈侃,沈聪祖,殷玥,张洁4,何磊,何磊
0010f69ef6f04d0ab0118ebd336c5454|程阔,王晓康1,玄丽薇,傅燎华,刘爽1,吴万龙
001ca702982a4311bd4dd8278738a310|周薇,张国庆,张聚实,谷世友,张洁4,常春林,高超伟,谷世友
0010f69ef6f04d0ab0118ebd336c5454|程阔,王晓康1,玄丽薇,傅燎华,刘爽1,吴万龙







--输出JSON 字符串
SELECT  TOP 10 InvoiceCategory,InvoiceType FROM T_BILL_INVOICE
FOR JSON PATH('')




--输出XML
SELECT  TOP 10 InvoiceCategory,InvoiceType FROM T_BILL_INVOICE
FOR XML PATH('')





 
SELECT ''''+ CAST( AA.ProgramID AS VARCHAR(1000) )   +''','
 FROM (
 select * from (
 select a.ProgramID,
 (select top 1 c.ParentProgramID from Sys_ProgramList c where a.ParentProgramID=c.ProgramID) ParentProgramID
 from Sys_ProgramList a inner join Sys_PageMain b on a.ProgramID=b.ProgramID
 where ISNULL(a.IsShow,0)=1 and isnull(a.IsBillPage,0)=1 and b.ListProgramID<>b.ProgramID) BillMain 
 where isnull(ParentProgramID,0)<>0
) AS AA
FOR XML PATH('')


	select  STUFF((
    SELECT 
      '{"businessCode":'+ cast(billno as varchar(20)) +'"'
	  + ',"operatorCode":'+(select  cUserCode from Sys_User WHERE  iUserID =  Bywho)+'"'
	  +',"auditStatus":PASS"'+'"'
	  +',"approvalTxt":审批通过"'+'"'
	  + ',"auditDate":'+ CONVERT(varchar(100), endtime, 21) +'"'
	  +'}'
	  FROM  DJLXSZ60_log  WHERE  node = 18 and nodestatus = 5 and  endtime > '2019-4-24'   order by endtime desc 
	  for xml path(''), type
    ).value('.', 'varchar(max)'), 1, 1, '')  



posted @ 2019-07-26 17:38  无敌土豆  阅读(651)  评论(0编辑  收藏  举报