sp_xml_preparedocument

use testdb
declare @cAccountData varchar(max)
set @cAccountData ='<accounts>
      <account IntKey="1"/>
      <account IntKey ="2"/>
      <account IntKey="3"/>
     </accounts>'
declare @hdoc int
EXEC sp_xml_preparedocument @hdoc output,@cAccountData

declare @taccounts table(IntKey varchar(20))
insert into @taccounts(IntKey)
select IntKey from openXML(@hdoc,'/accounts/account',1) with(IntKey varchar(20))
select * from @taccounts
Exec sp_xml_removedocument @hdoc

 

----another example

 

declare @cXMLDoc XML
declare @hdoc int
set @cXMLDoc ='<customer>
      <customernum>48456</customernum>
      <firstname>Kevin</firstname>
      <lastname>Goff</lastname>
      <address>
                  <city>Allentown</city>
      <state>PA</state>
      </address>
               </customer>'
EXEC sp_xml_preparedocument @hdoc output , @cXMLDoc
declare @tTemp table (customernum int ,firstname char(50)
,lastname char(50),city char(50),state char(10))

insert into @tTemp
select * from openXML(@hdoc,'/customer',2)
with(customernum int ,firstname varchar(50),lastname varchar(50),
city varchar(50) './address/city',
state varchar(50) './address/state')

select * from @tTemp

 

 

use testdb
declare @cXMLDoc XML
declare @hdoc int
set @cXMLDoc ='<root>
      <customer>
       <customernum>48456</customernum>
       <firstname>Kevin</firstname>
       <lastname>Goff</lastname>
       <address>
       <city>Allentown</city>
       <state>PA</state>
       </address>
     </customer>
       <customer>
       <customernum>123456</customernum>
       <firstname>gsun</firstname>
       <lastname>Lina</lastname>
       <address>
       <city>twotown</city>
       <state>PA</state>
       </address>
       </customer>
    </root>'
EXEC sp_xml_preparedocument @hdoc output , @cXMLDoc
declare @tTemp table (customernum int ,firstname char(50)
,lastname char(50),city char(50),state char(10))

insert into @tTemp
select * from openXML(@hdoc,'/root/customer',2)
with(customernum int './customernum',firstname varchar(50) './firstname',lastname varchar(50) './lastname',
city varchar(50) './address/city',
state varchar(50) './address/state')
select * from @tTemp

 

 

declare @tTest table ( Address xml)
insert into @tTest values('<Address>
 <addrRecord
 accountID="1"
 Street="31 main Dr"
 City="Newark"
 State="NJ"
 Zip="11111"/>
 </Address>')

 insert into @tTest values('<Address>
 <AddrRecord
     accountID ="2"  
  Street="1 Wilson Rd"
  City="Philly"
 State="PA"
 Zip="22222"/>
    </Address>')

select * from @tTest where Address.exist('/Address/AddrRecord [contains(@city,"hil")]')<>1

 

 

你可能感兴趣的:(document)