XML格式如下:
<ROOT>
<SM>
<SMID>88</SMID>
<SMCD>A5</SMCD>
<SMNM>刘洋</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>88</NDID>
<NDCD>A5</NDCD>
<NDNM>林林</NDNM>
<CDST>
<tR1>1</tR1>
<tR2>2</tR2>
<tR3>3</tR3>
<tR4>4</tR4>
<tR5>5</tR5>
<tR6>6</tR6>
<tR7>7</tR7>
<tR8>8</tR8>
<tR9>9</tR9>
<tt2>1</tt2>
<tt130>2</tt130>
<t132>3</t132>
<t155>4</t155>
</CDST>
</ND>
</NDS>
</SM>
<SM>
<SMID>84</SMID>
<SMCD>A6</SMCD>
<SMNM>陈真</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>84</NDID>
<NDCD>A6</NDCD>
<NDNM>陈真</NDNM>
<CDST>
<tR2>9</tR2>
<tR3>8</tR3>
<tR4>7</tR4>
<tR5>6</tR5>
<tR6>5</tR6>
<tR7>4</tR7>
<tR8>3</tR8>
<tR9>2</tR9>
<t12dd>1</t12dd>
<tt2>2</tt2>
<tt130>3</tt130>
<t132>4</t132>
<t155>5</t155>
</CDST>
</ND>
</NDS>
</SM>
<SM>
<SMID>45</SMID>
<SMCD>D1</SMCD>
<SMNM>曹曹</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>45</NDID>
<NDCD>D1</NDCD>
<NDNM>曹曹</NDNM>
<CDST>
<tR2>1</tR2>
</CDST>
</ND>
</NDS>
</SM>
</ROOT>
目的:
按照<NDID>节点的值取出其下<CDST>节点中所有的节点名称
如: NDID节点值为84(<NDID>84</NDID>)列出以下数据生成临时表#ItemCardType:
---------------
CardType
---------------
tR2
tR3
tR4
tR5
tR6
tR7
tR8
tR9
t12dd
tt2
tt130
t132
t155本人对XML操作不熟,请大家指教,谢谢~
<ROOT>
<SM>
<SMID>88</SMID>
<SMCD>A5</SMCD>
<SMNM>刘洋</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>88</NDID>
<NDCD>A5</NDCD>
<NDNM>林林</NDNM>
<CDST>
<tR1>1</tR1>
<tR2>2</tR2>
<tR3>3</tR3>
<tR4>4</tR4>
<tR5>5</tR5>
<tR6>6</tR6>
<tR7>7</tR7>
<tR8>8</tR8>
<tR9>9</tR9>
<tt2>1</tt2>
<tt130>2</tt130>
<t132>3</t132>
<t155>4</t155>
</CDST>
</ND>
</NDS>
</SM>
<SM>
<SMID>84</SMID>
<SMCD>A6</SMCD>
<SMNM>陈真</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>84</NDID>
<NDCD>A6</NDCD>
<NDNM>陈真</NDNM>
<CDST>
<tR2>9</tR2>
<tR3>8</tR3>
<tR4>7</tR4>
<tR5>6</tR5>
<tR6>5</tR6>
<tR7>4</tR7>
<tR8>3</tR8>
<tR9>2</tR9>
<t12dd>1</t12dd>
<tt2>2</tt2>
<tt130>3</tt130>
<t132>4</t132>
<t155>5</t155>
</CDST>
</ND>
</NDS>
</SM>
<SM>
<SMID>45</SMID>
<SMCD>D1</SMCD>
<SMNM>曹曹</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>45</NDID>
<NDCD>D1</NDCD>
<NDNM>曹曹</NDNM>
<CDST>
<tR2>1</tR2>
</CDST>
</ND>
</NDS>
</SM>
</ROOT>
目的:
按照<NDID>节点的值取出其下<CDST>节点中所有的节点名称
如: NDID节点值为84(<NDID>84</NDID>)列出以下数据生成临时表#ItemCardType:
---------------
CardType
---------------
tR2
tR3
tR4
tR5
tR6
tR7
tR8
tR9
t12dd
tt2
tt130
t132
t155本人对XML操作不熟,请大家指教,谢谢~
解决方案 »
- 怎么在SQL语句中动态处理表名?
- 求一树形结构的sql
- 遇到一个非常奇怪的问题,我试图把问题的描述的清楚些,急寻高手帮忙,语句已经写好了,需要指点一下
- 拆列 并分组加序号?
- !!请问大伙:怎么把一个字段的查询结果当另外一个字段的标题?!!!
- 如何复制表中纪录
- 在VS.NET 2005调试模式下访问SQL Server 2005的存储过程没有问题,在发布网站后,访问出现问题.
- 急救!两台sql server DTC服务频繁自动停止,求救
- 救命!DELPHI如何浏览SQL SERVER7.0的IMAGE字段,
- 新手求救!!该列没有包含在聚合函数或GROUP BY 子句中不着调如何处理。。。
- 按日期统计.
- 取出重复记录中的一条
---------------------------------------------------------------
-- DESIGNER :happycell188(喜喜)
-- QQ :584738179
-- Development Tool :Microsoft Visual C++ 6.0 C Language
-- FUNCTION :CONVERT DATA TO T-SQL
---------------------------------------------------------------
-- Microsoft SQL Server 2005
-- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
---------------------------------------------------------------
---------------------------------------------------------------use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
id int identity(1,1),
xml char(20)
)
go
--插入测试数据
insert into tb select '<ROOT>'
union all select '<SM>'
union all select '<SMID>88</SMID>'
union all select '<SMCD>A5</SMCD>'
union all select '<SMNM>刘洋</SMNM>'
union all select '<NDS>'
union all select '<ND>'
union all select '<SF>3</SF>'
union all select '<NDID>88</NDID>'
union all select '<NDCD>A5</NDCD>'
union all select '<NDNM>林林</NDNM>'
union all select '<CDST>'
union all select '<tR1>1</tR1>'
union all select '<tR2>2</tR2>'
union all select '<tR3>3</tR3>'
union all select '<tR4>4</tR4>'
union all select '<tR5>5</tR5>'
union all select '<tR6>6</tR6>'
union all select '<tR7>7</tR7>'
union all select '<tR8>8</tR8>'
union all select '<tR9>9</tR9>'
union all select '<tt2>1</tt2>'
union all select '<tt130>2</tt130>'
union all select '<t132>3</t132>'
union all select '<t155>4</t155>'
union all select '</CDST>'
union all select '</ND>'
union all select '</NDS>'
union all select '</SM>'
union all select '<SM>'
union all select '<SMID>84</SMID>'
union all select '<SMCD>A6</SMCD>'
union all select '<SMNM>陈真</SMNM>'
union all select '<NDS>'
union all select '<ND>'
union all select '<SF>3</SF>'
union all select '<NDID>84</NDID>'
union all select '<NDCD>A6</NDCD>'
union all select '<NDNM>陈真</NDNM>'
union all select '<CDST>'
union all select '<tR2>9</tR2>'
union all select '<tR3>8</tR3>'
union all select '<tR4>7</tR4>'
union all select '<tR5>6</tR5>'
union all select '<tR6>5</tR6>'
union all select '<tR7>4</tR7>'
union all select '<tR8>3</tR8>'
union all select '<tR9>2</tR9>'
union all select '<t12dd>1</t12dd>'
union all select '<tt2>2</tt2>'
union all select '<tt130>3</tt130>'
union all select '<t132>4</t132>'
union all select '<t155>5</t155>'
union all select '</CDST>'
union all select '</ND>'
union all select '</NDS>'
union all select '</SM>'
union all select '<SM>'
union all select '<SMID>45</SMID>'
union all select '<SMCD>D1</SMCD>'
union all select '<SMNM>曹曹</SMNM>'
union all select '<NDS>'
union all select '<ND>'
union all select '<SF>3</SF>'
union all select '<NDID>45</NDID>'
union all select '<NDCD>D1</NDCD>'
union all select '<NDNM>曹曹</NDNM>'
union all select '<CDST>'
union all select '<tR2>1</tR2>'
union all select '</CDST>'
union all select '</ND>'
union all select '</NDS>'
union all select '</SM>'
union all select '</ROOT>'
go
--代码实现
select isnull(NDID,'')NDID,isnull(CardType,'')CardType from(
select id,case when xml like '<NDID>%' then substring(xml,charindex('>',substring(xml,2,20))+2,2)end NDID,
case when xml like '<t%>%' then substring(xml,2,charindex('>',substring(xml,2,20))-1)end CardType
from tb )t
where NDID is not null or CardType is not null/*结果NDID CardType
-----------------
88
tR1
tR2
tR3
tR4
tR5
tR6
tR7
tR8
tR9
tt2
tt130
t132
t155
84
tR2
tR3
tR4
tR5
tR6
tR7
tR8
tR9
t12dd
tt2
tt130
t132
t155
45
tR2(30行受影响)
*/
set @x='
<ROOT>
<SM>
<SMID>88</SMID>
<SMCD>A5</SMCD>
<SMNM>刘洋</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>88</NDID>
<NDCD>A5</NDCD>
<NDNM>林林</NDNM>
<CDST>
<tR1>1</tR1>
<tR2>2</tR2>
<tR3>3</tR3>
<tR4>4</tR4>
<tR5>5</tR5>
<tR6>6</tR6>
<tR7>7</tR7>
<tR8>8</tR8>
<tR9>9</tR9>
<tt2>1</tt2>
<tt130>2</tt130>
<t132>3</t132>
<t155>4</t155>
</CDST>
</ND>
</NDS>
</SM>
<SM>
<SMID>84</SMID>
<SMCD>A6</SMCD>
<SMNM>陈真</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>84</NDID>
<NDCD>A6</NDCD>
<NDNM>陈真</NDNM>
<CDST>
<tR2>9</tR2>
<tR3>8</tR3>
<tR4>7</tR4>
<tR5>6</tR5>
<tR6>5</tR6>
<tR7>4</tR7>
<tR8>3</tR8>
<tR9>2</tR9>
<t12dd>1</t12dd>
<tt2>2</tt2>
<tt130>3</tt130>
<t132>4</t132>
<t155>5</t155>
</CDST>
</ND>
</NDS>
</SM>
<SM>
<SMID>45</SMID>
<SMCD>D1</SMCD>
<SMNM>曹曹</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>45</NDID>
<NDCD>D1</NDCD>
<NDNM>曹曹</NDNM>
<CDST>
<tR2>1</tR2>
</CDST>
</ND>
</NDS>
</SM>
</ROOT>'
sql xml 入门:
--by jinjazz
--http://blog.csdn.net/jinjazz
1、xml: 能认识元素、属性和值
2、xpath: 寻址语言,类似windows目录的查找(没用过dir命令的话就去面壁)
语法格式,这些语法可以组合为条件:
"."表示自己,".."表示父亲,"/"表示儿子,"//"表示后代,
"name"表示按名字查找,"@name"表示按属性查找
"集合[条件]" 表示根据条件取集合的子集,条件可以是
数 值:数字,last(),last()-数字 等
布尔值:position()<数字,@name='条件',name='条件'
条件是布尔值的时候可以合并计算:and or
3、xquery: 基于xpath标的准查询语言,sqlserver xquery包含如下函数
exist(xpath条件):返回布尔值表示节点是否存在
query(xpath条件):返回由符合条件的节点组成的新的xml文档
value(xpath条件,数据类型):返回指定的标量值,xpath条件结果必须唯一
nodes(xpath条件): 返回由符合条件的节点组成的一行一列的结果表
*/declare @data xml
set @data='
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="jp">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="cn">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
'--测试语句,如果不理解语法请参考上面的xpath规则和xquery函数说明--1、文档
select @data
--2、任意级别是否存在price节点
select @data.exist('//price')
--3、获取所有book节点
select @data.query('//book')
--4、获取所有包含lang属性的节点
select @data.query('//*[@lang]')
--5、获取第一个book节点
select @data.query('//book[1]')
--6、获取前两个book节点
select @data.query('//book[position()<=2]')
--7、获取最后一个book节点
select @data.query('//book[last()]')
--8、获取price>35的所有book节点
select @data.query('//book[price>35]')
--9、获取category="WEB"的所有book节点
select @data.query('//book[@category="WEB"]')
--10、获取title的lang="en"的所有book节点
select @data.query('//book/title[@lang="en"]')
--11、获取title的lang="en"且 price>35的所有book节点
select @data.query('//book[./title[@lang="en"] or price>35 ]')
--12、获取title的lang="en"且 price>35的第一book的(第一个)title
select @data.query('//book[./title[@lang="en"] and price>35 ]').value('(book/title)[1]','varchar(max)')
--13、等价于12
select @data.value('(//book[./title[@lang="en"] and price>35 ]/title)[1]','varchar(max)')
--14、获取title的lang="en"且 price>35的第一book的(第一个)title的lang属性
select @data.value('((//book[@category="WEB" and price>35 ]/title)[1]/@lang)[1]','varchar(max)')
--15、获取第一本书的title
select Tab.Col.value('(book/title)[1]','varchar(max)') as title
from @data.nodes('bookstore')as Tab(Col)
--16、获取每本书的第一个author
select Tab.Col.value('author[1]','varchar(max)') as title
from @data.nodes('//book')as Tab(Col)
--17、获取所有book的所有信息
select
T.C.value('title[1]','varchar(max)') as title,
T.C.value('year[1]','int') as year,
T.C.value('title[1]','varchar(max)')as title,
T.C.value('price[1]','float') as price,
T.C.value('author[1]','varchar(max)') as author1,
T.C.value('author[2]','varchar(max)') as author2,
T.C.value('author[3]','varchar(max)') as author3,
T.C.value('author[4]','varchar(max)') as author4
from @data.nodes('//book') as T(C)
--18、获取不是日语(lang!="jp")且价格大于35的书的所有信息
select
T.C.value('title[1]','varchar(max)') as title,
T.C.value('year[1]','int') as year,
T.C.value('title[1]','varchar(max)')as title,
T.C.value('price[1]','float') as price,
T.C.value('author[1]','varchar(max)') as author1,
T.C.value('author[2]','varchar(max)') as author2,
T.C.value('author[3]','varchar(max)') as author3,
T.C.value('author[4]','varchar(max)') as author4
from @data.nodes('//book[./title[@lang!="jp"] and price>35 ]') as T(C)
SET @xml='<ROOT>
<SM>
<SMID>88</SMID>
<SMCD>A5</SMCD>
<SMNM>刘洋</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>88</NDID>
<NDCD>A5</NDCD>
<NDNM>林林</NDNM>
<CDST>
<tR1>1</tR1>
<tR2>2</tR2>
<tR3>3</tR3>
<tR4>4</tR4>
<tR5>5</tR5>
<tR6>6</tR6>
<tR7>7</tR7>
<tR8>8</tR8>
<tR9>9</tR9>
<tt2>1</tt2>
<tt130>2</tt130>
<t132>3</t132>
<t155>4</t155>
</CDST>
</ND>
</NDS>
</SM>
<SM>
<SMID>84</SMID>
<SMCD>A6</SMCD>
<SMNM>陈真</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>84</NDID>
<NDCD>A6</NDCD>
<NDNM>陈真</NDNM>
<CDST>
<tR2>9</tR2>
<tR3>8</tR3>
<tR4>7</tR4>
<tR5>6</tR5>
<tR6>5</tR6>
<tR7>4</tR7>
<tR8>3</tR8>
<tR9>2</tR9>
<t12dd>1</t12dd>
<tt2>2</tt2>
<tt130>3</tt130>
<t132>4</t132>
<t155>5</t155>
</CDST>
</ND>
</NDS>
</SM>
<SM>
<SMID>45</SMID>
<SMCD>D1</SMCD>
<SMNM>曹曹</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>45</NDID>
<NDCD>D1</NDCD>
<NDNM>曹曹</NDNM>
<CDST>
<tR2>1</tR2>
</CDST>
</ND>
</NDS>
</SM>
</ROOT>'declare @xmla xml
set @[email protected]('for $s in//ND[NDID=84]/CDST/* return <a>{local-name($s)}</a>')
select T.C.value('.','varchar(20)') from @xmla.nodes('//a') as T(C)
--result
/*--------------------
tR2
tR3
tR4
tR5
tR6
tR7
tR8
tR9
t12dd
tt2
tt130
t132
t155(13 行受影响)*/
set @x='
<ROOT>
<SM>
<SMID>88</SMID>
<SMCD>A5</SMCD>
<SMNM>刘洋</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>88</NDID>
<NDCD>A5</NDCD>
<NDNM>林林</NDNM>
<CDST>
<tR1>1</tR1>
<tR2>2</tR2>
<tR3>3</tR3>
<tR4>4</tR4>
<tR5>5</tR5>
<tR6>6</tR6>
<tR7>7</tR7>
<tR8>8</tR8>
<tR9>9</tR9>
<tt2>1</tt2>
<tt130>2</tt130>
<t132>3</t132>
<t155>4</t155>
</CDST>
</ND>
</NDS>
</SM>
<SM>
<SMID>84</SMID>
<SMCD>A6</SMCD>
<SMNM>陈真</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>84</NDID>
<NDCD>A6</NDCD>
<NDNM>陈真</NDNM>
<CDST>
<tR2>9</tR2>
<tR3>8</tR3>
<tR4>7</tR4>
<tR5>6</tR5>
<tR6>5</tR6>
<tR7>4</tR7>
<tR8>3</tR8>
<tR9>2</tR9>
<t12dd>1</t12dd>
<tt2>2</tt2>
<tt130>3</tt130>
<t132>4</t132>
<t155>5</t155>
</CDST>
</ND>
</NDS>
</SM>
<SM>
<SMID>45</SMID>
<SMCD>D1</SMCD>
<SMNM>曹曹</SMNM>
<NDS>
<ND>
<SF>3</SF>
<NDID>45</NDID>
<NDCD>D1</NDCD>
<NDNM>曹曹</NDNM>
<CDST>
<tR2>1</tR2>
</CDST>
</ND>
</NDS>
</SM>
</ROOT>'select
t.x.value('local-name(.)','varchar(200)') as 节点名称,
t.x.value('.','varchar(200)') AS 节点值
from @x.nodes('//ND[./NDID = "84"]/CDST/*') as t(x);