SQL解析XML具体元素的属性
DECLARE @myDoc XML
DECLARE @imyDoc INT
SET @myDoc = '<IpList>
<StartIP>1.1.1.1</StartIP>
<EndIP>2.2.2.2</EndIP>
</IpList>'
EXECUTE SP_XML_PREPAREDOCUMENT @imyDoc OUTPUT, @myDoc
Create table #XMLa
(
StartIP varchar(100),
EndIP varchar(500)
)
INSERT INTO #XMLa SELECT *
FROM OPENXML (@imyDoc, '@myDoc', 1)
WITH(StartIP char(100) 'StartIP',EndIP varchar(100) 'EndIP')
EXECUTE SP_XML_REMOVEDOCUMENT @imyDoc
go
select * from #XMLa
drop table #XMLa
<EthernetLink>
<HubSpoke>H</HubSpoke>
<IsCustomerWANIP>N</IsCustomerWANIP>
<LAN_IP NormalRADCE="I" />
<RAD_CE_IP Type="Yes" NormalRADCE="R" />
<MegapopIPAddress>
<InternetProtocol>IPv4</InternetProtocol>
<WanIP>Yes</WanIP>
<NetworkInformation>Static IP</NetworkInformation>
<IpList>
<IP StartIP="1.1.1.1" EndIP="2.2.2.2" />
</IpList>
<WanIpProviderEndIP>3.3.3.3</WanIpProviderEndIP>
<WanIpCustomerEndIP>4.4.4.4</WanIpCustomerEndIP>
<IsIsp>false</IsIsp>
<Bgp>false</Bgp>
</MegapopIPAddress>
</EthernetLink>"
declare @myDoc xml
set @myDoc = '<EthernetLink>
<HubSpoke>H</HubSpoke>
<IsCustomerWANIP>N</IsCustomerWANIP>
<LAN_IP NormalRADCE="I"></LAN_IP>
<RAD_CE_IP Type="Yes" NormalRADCE="R"></RAD_CE_IP>
<MegapopIPAddress>
<InternetProtocol>IPv4</InternetProtocol>
<WanIP>Yes</WanIP>
<NetworkInformation>Static IP</NetworkInformation>
<IpList>
<IP StartIP="1.1.1.1" EndIP="2.2.2.2" />
</IpList>
<WanIpProviderEndIP>3.3.3.3</WanIpProviderEndIP>
<WanIpCustomerEndIP>4.4.4.4</WanIpCustomerEndIP>
<IsIsp>false</IsIsp>
<Bgp>false</Bgp>
</MegapopIPAddress>
</EthernetLink>'
create table #XMLa
(StartIP varchar(20),
EndIP varchar(20))
insert into #XMLa
select T.c.value('@StartIP[1]', 'varchar(20)'),
T.c.value('@EndIP[1]', 'varchar(20)')
from @myDoc.nodes('/EthernetLink/MegapopIPAddress/IpList/IP') T(c)
select * from #XMLa
/*
StartIP EndIP
-------------------- --------------------
1.1.1.1 2.2.2.2
(1 row(s) affected)
*/