DECLARE @x XML
SELECT @x = '
<People>
<dongsheng id="2">
<Info Name="Email">dongsheng@xxyy.com</Info>
<Info Name="Phone">678945546</Info>
<Info Name="qq" mark="中国">36575</Info>
</dongsheng>
<dongsheng id="1">
<Info Name="Email">cctv@163.com</Info>
<Info Name="Phone">666666</Info>
<Info Name="qq">346667</Info>
</dongsheng>
</People>'
-- 1
--SELECT @x.value('data(/People/dongsheng/Info[@Name="Email"])[1]', 'varchar(30)')
--SELECT @x.value('data(/People/dongsheng/Info[@Name="Email"])[2]', 'varchar(30)')
-- 2
--SELECT @x.value('(/People/dongsheng/Info[@Name="Email"])[1]', 'varchar(30)')
--SELECT @x.value('(/People/dongsheng/Info[@Name="Email"])[2]', 'varchar(30)')
-- 3
--SELECT C.value('.','varchar(30)') FROM @x.nodes('/People/dongsheng/Info[@Name="Email"]') T(C)
/*
SELECT @x.value('.', 'varchar(30)')
/*
dongsheng@xxyy.com678945546365
*/
*/
/*
SELECT @x.value('.', 'varchar(30)')
/*
dongsheng@xxyy.com678945546365
*/
*/
/*
SELECT C.value('.','varchar(30)') FROM @x.nodes('/People/dongsheng/Info[@Name="Email"]') T(C)
/*
dongsheng@xxyy.com
cctv@163.com
*/
*/
/* xml.nodes产生表 需指定表名和列
SELECT C.value('.','varchar(30)') FROM @x.nodes('/People/dongsheng/Info') T(C)
/*
dongsheng@xxyy.com
678945546
36575
cctv@163.com
666666
346667
*/
*/
/*
SELECT C.value('.','varchar(30)') FROM @x.nodes('/People/dongsheng/Info') T(C)
SELECT C.value('.','varchar(30)') FROM @x.nodes('/People') T(C)
*/
-- 4
--SELECT C.value('(Info[@Name="Email"])[1]','varchar(30)') FROM @x.nodes('/People/dongsheng') T(C)
/*
dongsheng@xxyy.com
cctv@163.com
*/
-- 5
--SELECT C.value('(dongsheng/Info[@Name="Email"])[1]','varchar(30)') FROM @x.nodes('/People') T(C)
-- 6
----SELECT C.value('.','varchar(30)')
----FROM @x.nodes('/People/dongsheng/Info') T(C)
----WHERE C.value('(.[@Name="Email"])[1]','varchar(30)') IS NOT NULL
/*
dongsheng@xxyy.com
cctv@163.com
*/
-- 7
--SELECT C.value('.','varchar(30)')
--FROM @x.nodes('/People/dongsheng/Info') T(C)
--WHERE C.exist('(.[@Name="Email"])[1]') = 1
/*
dongsheng@xxyy.com
cctv@163.com
*/
SELECT @x = '
<People>
<dongsheng id="2">
<Info Name="Email">dongsheng@xxyy.com</Info>
<Info Name="Phone">678945546</Info>
<Info Name="qq" mark="中国">36575</Info>
</dongsheng>
<dongsheng id="1">
<Info Name="Email">cctv@163.com</Info>
<Info Name="Phone">666666</Info>
<Info Name="qq">346667</Info>
</dongsheng>
</People>'
-- 1
--SELECT @x.value('data(/People/dongsheng/Info[@Name="Email"])[1]', 'varchar(30)')
--SELECT @x.value('data(/People/dongsheng/Info[@Name="Email"])[2]', 'varchar(30)')
-- 2
--SELECT @x.value('(/People/dongsheng/Info[@Name="Email"])[1]', 'varchar(30)')
--SELECT @x.value('(/People/dongsheng/Info[@Name="Email"])[2]', 'varchar(30)')
-- 3
--SELECT C.value('.','varchar(30)') FROM @x.nodes('/People/dongsheng/Info[@Name="Email"]') T(C)
/*
SELECT @x.value('.', 'varchar(30)')
/*
dongsheng@xxyy.com678945546365
*/
*/
/*
SELECT @x.value('.', 'varchar(30)')
/*
dongsheng@xxyy.com678945546365
*/
*/
/*
SELECT C.value('.','varchar(30)') FROM @x.nodes('/People/dongsheng/Info[@Name="Email"]') T(C)
/*
dongsheng@xxyy.com
cctv@163.com
*/
*/
/* xml.nodes产生表 需指定表名和列
SELECT C.value('.','varchar(30)') FROM @x.nodes('/People/dongsheng/Info') T(C)
/*
dongsheng@xxyy.com
678945546
36575
cctv@163.com
666666
346667
*/
*/
/*
SELECT C.value('.','varchar(30)') FROM @x.nodes('/People/dongsheng/Info') T(C)
SELECT C.value('.','varchar(30)') FROM @x.nodes('/People') T(C)
*/
-- 4
--SELECT C.value('(Info[@Name="Email"])[1]','varchar(30)') FROM @x.nodes('/People/dongsheng') T(C)
/*
dongsheng@xxyy.com
cctv@163.com
*/
-- 5
--SELECT C.value('(dongsheng/Info[@Name="Email"])[1]','varchar(30)') FROM @x.nodes('/People') T(C)
-- 6
----SELECT C.value('.','varchar(30)')
----FROM @x.nodes('/People/dongsheng/Info') T(C)
----WHERE C.value('(.[@Name="Email"])[1]','varchar(30)') IS NOT NULL
/*
dongsheng@xxyy.com
cctv@163.com
*/
-- 7
--SELECT C.value('.','varchar(30)')
--FROM @x.nodes('/People/dongsheng/Info') T(C)
--WHERE C.exist('(.[@Name="Email"])[1]') = 1
/*
dongsheng@xxyy.com
cctv@163.com
*/