Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
XML
<test>
  <DBO.JOB>
    <JOB_NO>234234</JOB_NO>
    <CREW_NO>64850</CREW_NO>
    <BEGINDATE></BEGINDATE>
    <ENDDATE></ENDDATE>
  </DBO.JOB>
  <DBO.JOB>
    <JOB_NO>234</JOB_NO>
    <CREW_NO>234</CREW_NO>
    <BEGINDATE></BEGINDATE>
    <ENDDATE></ENDDATE>
  </DBO.JOB>
  <DBO.JOB>
    <JOB_NO>324</JOB_NO>
    <CREW_NO>234234</CREW_NO>
    <BEGINDATE></BEGINDATE>
    <ENDDATE></ENDDATE>
  </DBO.JOB>
</test>




I want to know how to parse these tags using only OPEN XML and update them to a table DBO.JOB
Posted

1 solution

Straight from BOL SQLSERVER2K8 ... "OPENXML" (/w substitution):
DECLARE @idoc int
DECLARE @doc varchar(1000)

SET @doc ='<test>
		 <dbo.job>
			<job_no>234234</job_no>
			<crew_no>64850</crew_no>
			<begindate></begindate>
			<enddate></enddate>
		  </dbo.job>
		  <dbo.job>
			<job_no>234</job_no>
			<crew_no>234</crew_no>
			<begindate></begindate>
			<enddate></enddate>
		  </dbo.job>
		  <dbo.job>
			<job_no>324</job_no>
			<crew_no>234234</crew_no>
			<begindate></begindate>
			<enddate></enddate>
		  </dbo.job>
	</test>	'

That's the look to the data.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
SELECT    *
FROM       OPENXML (@idoc, '/test/DBO.JOB',2)
            WITH (JOB_NO  int,
                  CREW_NO int,
                  BEGINDATE datetime,
                  ENDDATE datetime)

The "OPENXML" help states "Execute a SELECT statement that uses the OPENXML rowset provider". There is another way to do all this. But it's not via OPENXML. See "XPath". Same ... JUST as complicated.

The output:
JOB_NO	CREW_NO	BEGINDATE	         ENDDATE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
234234	64850	1900-01-01 00:00:00.000	 1900-01-01 00:00:00.000
234	234	1900-01-01 00:00:00.000	 1900-01-01 00:00:00.000
324	234234	1900-01-01 00:00:00.000	 1900-01-01 00:00:00.000
 
Share this answer
 
Comments
RedDk 7-Mar-13 13:56pm    
You'll probably want to put that output in a table you create. Then, in order to update the data do the appropriate UPDATE (usg TRIGGER, etc) ... Then output the XML.

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