Hi,
Here I am giving the example how to read XML fields in SQL Server query.
Database Table jobtable contains jobxml field which datatype is xml
You can query to read xml. It is very simple to use.
Suppose you have following XML :
<scheduler_job>
<submitter>
<email notify="false" id="5">pdehadray</email>
</submitter>
</scheduler_job>
Query to read email node inner text is as follows:
select jobxml.value('(/scheduler_job/submitter/email)[1]','varchar(50)') from jobtable
Query to read notify attribute value in email node is as follows:
select jobxml.value('(/scheduler_job/submitter/email/@notify)[1]','varchar(50)') from jobtable
Query to read id attribute value in email node is as follows:
select jobxml.value('(/scheduler_job/submitter/email/@id)[1]','int') from jobtable
If XML contains two node with same name like this:
<scheduler_job>
<submitter>
<email notify="false" id="5">pdehadray</email>
<email notify="true" id="6">rjaiswal</email>
</submitter>
</scheduler_job>
Query to read 1st email node inner text is as follows:
select jobxml.value('(/scheduler_job/submitter/email)[1]','varchar(50)') from jobtable
Query to read 2nd email node inner text is as follows:
select jobxml.value('(/scheduler_job/submitter/email)[2]','varchar(50)') from jobtable
Query to read notify attribute value in 1st email node is as follows:
select jobxml.value('(/scheduler_job/submitter/email/@notify)[1]','varchar(50)') from jobtable
Query to read notify attribute value in 2nd email node is as follows:
select jobxml.value('(/scheduler_job/submitter/email/@notify)[2]','varchar(50)') from jobtable
Query to read id attribute value in 1st email node is as follows:
select jobxml.value('(/scheduler_job/submitter/email/@id)[1]','int') from jobtable
Query to read id attribute value in 2nd email node is as follows:
select jobxml.value('(/scheduler_job/submitter/email/@id)[2]','int') from jobtable
Hope it will be useful for you.