MSSql表列拼接成字符串简析

来源:未知 责任编辑:责任编辑 发表时间:2014-05-26 11:01 点击:
MSSql表列拼接成字符串简析
 
将表列拼接成字符串。闲话不多说切入正题,看代码:
[sql]  
select  STUFF(  
        REPLACE(    www.2cto.com  
            REPLACE(  
                (select  fieldName as pc from cj_datadictionary as N  
                join [CSD_ORD_PayMent]  on costsid=fieldCode and fieldType='OP' where memberid=72157 and groupID='201205DD2119416553' and [Expenditure]>0   
                FOR XML AUTO),'<N pc="','+'  
            ),'"/>',''  
        ),1,1,''  
    )  
 
第一步:将表转化为xml格式
 
[sql]  
select  fieldName as pc from cj_datadictionary as N  
join [CSD_ORD_PayMent]  on costsid=fieldCode and fieldType='OP'   
where memberid=72157 and groupID='201205DD2119416553' and [Expenditure]>0   
FOR XML AUTO    www.2cto.com  
 
第二步:替换<N pc=" 注:N是表名,pc是列名
 
[sql]  
select REPLACE((select  fieldName as pc from cj_datadictionary as N  
join [CSD_ORD_PayMent]  on costsid=fieldCode and fieldType='OP'   
where memberid=72157 and groupID='201205DD2119416553' and [Expenditure]>0   
FOR XML AUTO),'<N pc="','+')  
 
第三步:替换 "/> 注:每项都会以“<表 字段”开始,以“ "/>”结束
 
[sql]  
select REPLACE(REPLACE((select  fieldName as pc from cj_datadictionary as N  
join [CSD_ORD_PayMent]  on costsid=fieldCode and fieldType='OP'   
where memberid=72157 and groupID='201205DD2119416553' and [Expenditure]>0   
FOR XML AUTO),'<N pc="','+'),'"/>','')  
 
最后使用:STUFF() 函数作用:删除指定长度的字符,并在指定的起点上插入另外的字符串
 
[sql]  
select  STUFF(  
        REPLACE(    www.2cto.com  
            REPLACE(  
                (select  fieldName as pc from cj_datadictionary as N  
                join [CSD_ORD_PayMent]  on costsid=fieldCode and fieldType='OP' where memberid=72157 and groupID='201205DD2119416553' and [Expenditure]>0   
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • sql常见面试题
  • SQL SERVER 2005性能之跟踪
  • SQL编程(一)
  • LINUX上RMAN自动备份脚本
  • sql server面试题
  • 如何将多个SQL查询统计结果一次显示出来
  • 浅谈SQL Server中的事务日志(三)----在简单恢复模式下日志的角色
  • sql server 列转行
  • SQL小技巧系列 --- 行转列合并
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1