看看下面这段代码的错误

sqlServer_csd 2013-10-18 04:46:42

if exists(select top(1)1 from sys.objects with(nolock) where name=N'tb' and type=N'U')
drop table tb
create table tb(hobbyid int,hname varchar(10))
insert into tb(hobbyid,hname)
select 1,N'爬山' union all
select 2,N'游泳' union all
select 3,N'吃货'

--应用FOR XML PATH查询语句执行
select * from tb with(nolock) for xml path
--结果:
/*
<row>
<hobbyid>1</hobbyid>
<hname>爬山</hname>
</row>
<row>
<hobbyid>2</hobbyid>
<hname>游泳</hname>
</row>
<row>
<hobbyid>3</hobbyid>
<hname>吃货</hname>
</row>
*/
--由此可见FOR XML PATH可以将查询结果根据行输出成XML格式
--那么,如何改变XML行节点的名称呢?代码如下:
select * from tb with(nolock) for xml path('行节点名称')
/*
<行节点名称>
<hobbyid>1</hobbyid>
<hname>爬山</hname>
</行节点名称>
<行节点名称>
<hobbyid>2</hobbyid>
<hname>游泳</hname>
</行节点名称>
<行节点名称>
<hobbyid>3</hobbyid>
<hname>吃货</hname>
</行节点名称>
*/
--原来的行节点<row></row>变成了path后面括号()中的内容,自定义的名称<行节点的名称></行节点的名称>
--那么,又如何改变列节点的名称呢?使用as为列改变名字!
select hobbyid as '第一列',hname as '第二列' from tb with(nolock) for xml path('行节点名称')
/*
<行节点名称>
<第一列>1</第一列>
<第二列>爬山</第二列>
</行节点名称>
<行节点名称>
<第一列>2</第一列>
<第二列>游泳</第二列>
</行节点名称>
<行节点名称>
<第一列>3</第一列>
<第二列>吃货</第二列>
</行节点名称>
*/
--下面来看如何构建输出方式
select '[ '+hname+' ]' from tb with(nolock) for xml path('')
/*
[ 爬山 ][ 游泳 ][ 吃货 ]
*/
select '{ '+str(hobbyid,1)+' }','[ '+hname+' ]' from tb with(nolock) for xml path('')
/*
{ 1 }[ 爬山 ]{ 2 }[ 游泳 ]{ 3 }[ 吃货 ]
*/

--------------------------
--下面看for xml path的应用
--增加一张兴趣爱好表
if exists(select top(1)1 from sys.objects with(nolock) where name=N'tb2' and type=N'U')
drop table tb2
create table tb2(iid int,sname varchar(10),hobby varchar(10))
insert into tb2(iid,sname,hobby)
select 1,N'张三',N'爬山' union all
select 2,N'张三',N'游泳' union all
select 3,N'李四',N'吃货' union all
select 4,N'李四',N'吃货' union all
select 5,N'王五',N'游泳' union all
select 6,N'王五',N'吃货'

select b.sname,LEFT(hlist,LEN(hlist)-1) as hobby from(
select sname,(select hobby+',' from tb2 with(nolock) where sname=a.sname for xml path(''))as hlist
from tb2 a
group by sname
)b

select sname
,hobby=(STUFF((select ','+hobby from tb2 with(nolock) where sname=a.sname for XML path('')),1,1,''))
from tb2 a with(nolock) group by sname

--取出重复的列的两种方法
select sname
,hobby1=(STUFF((select ','+hobby from tb2 with(nolock) where sname=a.sname for XML path('')),1,1,''))
,hobby2=(STUFF((select distinct ','+hobby from tb2 with(nolock) where sname=a.sname for XML path('')),1,1,''))
,hobby3=(STUFF((select ','+hobby from tb2 with(nolock) where sname=a.sname group by hobby for XML path('')),1,1,''))
from tb2 a with(nolock) group by sname

drop table tb
drop table tb2


...全文
59 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
-Tracy-McGrady- 2013-10-18
  • 打赏
  • 举报
回复
if exists(select top(1)1 from sys.objects with(nolock) where name=N'tb' and type=N'U') drop table tb create table tb(hobbyid int,hname varchar(10)) insert into tb(hobbyid,hname) select 1,N'爬山' union all select 2,N'游泳' union all select 3,N'吃货' --应用FOR XML PATH查询语句执行 select * from tb with(nolock) for xml path --结果: /* <row> <hobbyid>1</hobbyid> <hname>爬山</hname> </row> <row> <hobbyid>2</hobbyid> <hname>游泳</hname> </row> <row> <hobbyid>3</hobbyid> <hname>吃货</hname> </row> */ --由此可见FOR XML PATH可以将查询结果根据行输出成XML格式 --那么,如何改变XML行节点的名称呢?代码如下: select * from tb with(nolock) for xml path('行节点名称') /* <行节点名称> <hobbyid>1</hobbyid> <hname>爬山</hname> </行节点名称> <行节点名称> <hobbyid>2</hobbyid> <hname>游泳</hname> </行节点名称> <行节点名称> <hobbyid>3</hobbyid> <hname>吃货</hname> </行节点名称> */ --原来的行节点<row></row>变成了path后面括号()中的内容,自定义的名称<行节点的名称></行节点的名称> --那么,又如何改变列节点的名称呢?使用as为列改变名字! select hobbyid as '第一列',hname as '第二列' from tb with(nolock) for xml path('行节点名称') /* <行节点名称> <第一列>1</第一列> <第二列>爬山</第二列> </行节点名称> <行节点名称> <第一列>2</第一列> <第二列>游泳</第二列> </行节点名称> <行节点名称> <第一列>3</第一列> <第二列>吃货</第二列> </行节点名称> */ --下面来看如何构建输出方式 select '[ '+hname+' ]' from tb with(nolock) for xml path('') /* [ 爬山 ][ 游泳 ][ 吃货 ] */ select '{ '+str(hobbyid,1)+' }','[ '+hname+' ]' from tb with(nolock) for xml path('') /* { 1 }[ 爬山 ]{ 2 }[ 游泳 ]{ 3 }[ 吃货 ] */ -------------------------- --下面看for xml path的应用 --增加一张兴趣爱好表 if exists(select top(1)1 from sys.objects with(nolock) where name=N'tb2' and type=N'U') drop table tb2 create table tb2(iid int,sname varchar(10),hobby varchar(10)) insert into tb2(iid,sname,hobby) select 1,N'张三',N'爬山' union all select 2,N'张三',N'游泳' union all select 3,N'李四',N'吃货' union all select 4,N'李四',N'吃货' union all select 5,N'王五',N'游泳' union all select 6,N'王五',N'吃货' select b.sname,LEFT(hlist,LEN(hlist)-1) as hobby from( select sname,(select hobby+',' from tb2 with(nolock) where sname=a.sname for xml path(''))as hlist from tb2 a group by sname )b select sname ,hobby=(STUFF((select ','+hobby from tb2 with(nolock) where sname=a.sname for XML path('')),1,1,'')) from tb2 a with(nolock) group by sname --取出重复的列的两种方法 select sname ,hobby1=(STUFF((select ','+hobby from tb2 with(nolock) where sname=a.sname for XML path('')),1,1,'')) ,hobby2=(STUFF((select distinct ','+hobby from tb2 with(nolock) where sname=a.sname for XML path('')),1,1,'')) ,hobby3=(STUFF((select ','+hobby from tb2 with(nolock) where sname=a.sname group by hobby for XML path('')),1,1,'')) from tb2 a with(nolock) group by sname drop table tb drop table tb2

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧