For xml path

1、将数据库中的内容转换为XML文件格式

1.1 创建表tbl_Class

CREATE TABLE tbl_Class(
 ClassId int,
 ClassName nvarchar(50),
 StudentName nvarchar(50)
)
GO

1.2 插入数据
insert into tbl_Class (ClassId,ClassName,StudentName)values(1,'语文','张三')
insert into tbl_Class (ClassId,ClassName,StudentName)values(2,'数学','张三')
insert into tbl_Class (ClassId,ClassName,StudentName)values(3,'语文','李四')
insert into tbl_Class (ClassId,ClassName,StudentName)values(4,'数学','李四')
insert into tbl_Class (ClassId,ClassName,StudentName)values(5,'英语','李四')
insert into tbl_Class (ClassId,ClassName,StudentName)values(6,'物理','李四')
insert into tbl_Class (ClassId,ClassName,StudentName)values(7,'数学','王五')
1.3 sql语句

SELECT * FROM tbl_Class FOR XML PATH

1.4 执行结果

<row>
  <ClassId>1</ClassId>
  <ClassName>语文</ClassName>
  <StudentName>张三</StudentName>
</row>
<row>
  <ClassId>2</ClassId>
  <ClassName>数学</ClassName>
  <StudentName>张三</StudentName>
</row>
<row>
  <ClassId>3</ClassId>
  <ClassName>语文</ClassName>
  <StudentName>李四</StudentName>
</row>
<row>
  <ClassId>4</ClassId>
  <ClassName>数学</ClassName>
  <StudentName>李四</StudentName>
</row>
<row>
  <ClassId>5</ClassId>
  <ClassName>英语</ClassName>
  <StudentName>李四</StudentName>
</row>
<row>
  <ClassId>6</ClassId>
  <ClassName>物理</ClassName>
  <StudentName>李四</StudentName>
</row>
<row>
  <ClassId>7</ClassId>
  <ClassName>数学</ClassName>
  <StudentName>王五</StudentName>
</row>

1.5 更改行节点名称

SELECT * FROM tbl_Class FOR XML PATH('MySubjects')

<MySubjects>
  <ClassId>1</ClassId>
  <ClassName>语文</ClassName>
  <StudentName>张三</StudentName>
</MySubjects>
<MySubjects>
  <ClassId>2</ClassId>
  <ClassName>数学</ClassName>
  <StudentName>张三</StudentName>
</MySubjects>
<MySubjects>
  <ClassId>3</ClassId>
  <ClassName>语文</ClassName>
  <StudentName>李四</StudentName>
</MySubjects>
<MySubjects>
  <ClassId>4</ClassId>
  <ClassName>数学</ClassName>
  <StudentName>李四</StudentName>
</MySubjects>
<MySubjects>
  <ClassId>5</ClassId>
  <ClassName>英语</ClassName>
  <StudentName>李四</StudentName>
</MySubjects>
<MySubjects>
  <ClassId>6</ClassId>
  <ClassName>物理</ClassName>
  <StudentName>李四</StudentName>
</MySubjects>
<MySubjects>
  <ClassId>7</ClassId>
  <ClassName>数学</ClassName>
  <StudentName>王五</StudentName>
</MySubjects>

1.6 更改列节点名称

SELECT ClassId as myId,ClassName as subjects,StudentName as name FROM tbl_Class FOR XML PATH('MySubjects')

<MySubjects>
  <myId>1</myId>
  <subjects>语文</subjects>
  <name>张三</name>
</MySubjects>
<MySubjects>
  <myId>2</myId>
  <subjects>数学</subjects>
  <name>张三</name>
</MySubjects>
<MySubjects>
  <myId>3</myId>
  <subjects>语文</subjects>
  <name>李四</name>
</MySubjects>
<MySubjects>
  <myId>4</myId>
  <subjects>数学</subjects>
  <name>李四</name>
</MySubjects>
<MySubjects>
  <myId>5</myId>
  <subjects>英语</subjects>
  <name>李四</name>
</MySubjects>
<MySubjects>
  <myId>6</myId>
  <subjects>物理</subjects>
  <name>李四</name>
</MySubjects>
<MySubjects>
  <myId>7</myId>
  <subjects>数学</subjects>
  <name>王五</name>
</MySubjects>

1.7 一行输出

SELECT '['+STR(ClassId)+']','['+StudentName+']' FROM tbl_Class FOR XML PATH('') 

[ 1][张三][2][张三][3][李四][4][李四][5][李四][ 6][李四][ 7][王五]

2 应用

SELECT B.StudentName
 ,LEFT(Subjects, LEN(Subjects) - 1) AS Subjects
FROM (
 SELECT StudentName
  ,(
   SELECT ClassName + ','
   FROM tbl_Class
   WHERE StudentName = A.StudentName
   FOR XML PATH('')
   ) AS Subjects
 FROM tbl_Class A
 GROUP BY StudentName
 ) B
 
GO

将内容输出到一行

StudentName Subjects
李四 语文,数学,英语,物理
王五 数学
张三 语文,数学

你可能感兴趣的:(sql,for xml)