I have an xml document which i want to pass as a parameter to a stored procedure
the xml looks like this
<root>
<EMPLOYEE ID= 100>
<PERIOD>AUG-2010</PERIOD>
<earnings>
<title>BASIC</title>
<amount>2000</amount>
<title>HRA</title>
<amount>1000</amount>
<title>CONVEYANCE</title>
<amount>500</amount>
</earnings>
</EMPLOYEE>
<EMPLOYEE ID= 101>
<PERIOD>AUG-2010</PERIOD>
<earnings>
<title>BASIC</title>
<amount>2000</amount>
<title>HRA</title>
<amount>400</amount>
<title>CONVEYANCE</title>
<amount>500</amount>
</earnings>
</EMPLOYEE>
<EMPLOYEE ID= 102>
<PERIOD>AUG-2010</PERIOD>
<earnings>
<title>BASIC</title>
<amount>2000</amount>
<title>HRA</title>
<amount>800</amount>
<title>CONVEYANCE</title>
<amount>5000</amount>
</earnings>
</EMPLOYEE>
</root>
I need to store the above information to 2 tables ie: payslipdetails and payheaddetails.
I think I have to loop through the xml doc . the outer loop gives me the employee id and period then I insert into the payslipdetails table with those fields and then get into the inner loop and I want to insert the payheaddetailswith the same employeeid and all his earnings deatls like
empid title amount
100 basic 2000
100 hra 1000
100 conveyance 500
then I go to the outer loop and get the next employee id and repeat the same thing
How can I go to the inner child xml any ways like openxml etc..??
First of all, this is not valid XML:
The
ID=attribute must be followed immediately by data – preferably in double quotes – this is valid:Next: the fact that you have multiple
<title>..</title><amount>...</amount>tag pairs inside your<earnings>tag without a container around them makes parsing next to impossible (or really messy)…..If ever possible, try to change this to something like this:
That would be much easier to handle!
If you had that extra
<earning>container that encloses the<title>/<amount>pair, then you could easily write this XQuery statement and handle all your needs without messy, slow cursors alltogether:and you’d get an output something like this: