Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi how are you.

i need to read xml string in sqlserver. this is xmlfile
declare @stringxml xml

set @stringxml ='
<NewDataSet>
  <Table1>
    <groupname>Superannuation Pension</groupname>
    <effectivedate>11/02/2012</effectivedate>
    <termdate>11/02/2012</termdate>
    <groupvalue>PE</groupvalue>
  </Table1>
  <Table1>
    <groupname>Family Pension</groupname>
    <effectivedate>11/02/2012</effectivedate>
    <termdate>11/02/2012</termdate>
    <groupvalue>PF</groupvalue>
  </Table1>
  <Table1>
    <groupname>Judges Pension</groupname>
    <effectivedate>11/02/2012</effectivedate>
    <termdate>11/02/2012</termdate>
    <groupvalue>PJ</groupvalue>
  </Table1>
  <Table2>
    <groupname>Pension Arrear</groupname>
    <effectivedate>11/02/2012</effectivedate>
    <termdate>11/02/2012</termdate>
    <groupvalue>tbllocationPJ</groupvalue>
  </Table2>
  <Table2>
    <groupname>First Pension</groupname>
    <effectivedate>11/02/2012</effectivedate>
    <termdate>11/02/2012</termdate>
    <groupvalue>tbllocationPE</groupvalue>
  </Table2>
  <Table2>
    <groupname>Leave Travel Concession</groupname>
    <effectivedate>11/02/2012</effectivedate>
    <termdate>11/02/2012</termdate>
    <groupvalue>tbllocationPF</groupvalue>
  </Table2>
</NewDataSet>'



Actuallly i need
<Table1>
xml in temporary table and
<table2>
xml in another temporary table.


Please help me on this
Posted

1 solution

Try this, let me know if you have any query :)

declare @stringxml xml
 
set @stringxml ='
<NewDataSet>
  <Table1>
    <groupname>Superannuation Pension</groupname>
    <effectivedate>11/02/2012</effectivedate>
    <termdate>11/02/2012</termdate>
    <groupvalue>PE</groupvalue>
  </Table1>
  <Table1>
    <groupname>Family Pension</groupname>
    <effectivedate>11/02/2012</effectivedate>
    <termdate>11/02/2012</termdate>
    <groupvalue>PF</groupvalue>
  </Table1>
  <Table1>
    <groupname>Judges Pension</groupname>
    <effectivedate>11/02/2012</effectivedate>
    <termdate>11/02/2012</termdate>
    <groupvalue>PJ</groupvalue>
  </Table1>
  <Table2>
    <groupname>Pension Arrear</groupname>
    <effectivedate>11/02/2012</effectivedate>
    <termdate>11/02/2012</termdate>
    <groupvalue>tbllocationPJ</groupvalue>
  </Table2>
  <Table2>
    <groupname>First Pension</groupname>
    <effectivedate>11/02/2012</effectivedate>
    <termdate>11/02/2012</termdate>
    <groupvalue>tbllocationPE</groupvalue>
  </Table2>
  <Table2>
    <groupname>Leave Travel Concession</groupname>
    <effectivedate>11/02/2012</effectivedate>
    <termdate>11/02/2012</termdate>
    <groupvalue>tbllocationPF</groupvalue>
  </Table2>
</NewDataSet>'


Declare @intPointer int  
EXEC sp_xml_preparedocument @intPointer OUTPUT, @stringxml

SELECT * into #Table1 FROM OpenXml(@intPointer,'/NewDataSet/Table1',2)  
WITH (groupname varchar(100), effectivedate datetime, termdate datetime, groupvalue varchar(100))  

SELECT * FROM #Table1

EXEC sp_xml_removedocument @intPointer  

DROP TABLE #Table1
 
Share this answer
 
Comments
balongi 18-Jan-13 8:29am    
Thanks............
URVISH_SUTHAR1 18-Jan-13 8:47am    
Welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900