Solved

Update/INSERT data based on an XML?

Posted on 2014-10-01
1
118 Views
Last Modified: 2014-10-13
I have had some help in a previous question regarding how to update a table from an XML document.  The code is below.

It updates the ddt_value column given the documentid and ddt_name of the row.

However i have discovered that i also need to insert new rows if the updated row does not exist.

So if there is not a row for the documentId and ddt_Fieldname combination in the table i need to insert a new row with the documentid, ddt_Fieldname and the ddt_value.

Does this require me to traverse the xml document or can the existing sql be extended?

      with Data
      as ( select
	         TestForm1.value('Id[1]', 'INT') AS ID ,
             KeyValue.value('local-name(.)', 'NVARCHAR(255)') AS [Key] ,
             KeyValue.value('.', 'NVARCHAR(255)') AS [Value]
           from  @XmlData.nodes('/BRUM-registret/FormidableTest') A ( TestForm1 )
           cross apply TestForm1.nodes('*[local-name()!="Id"]') B ( KeyValue )
             )
      update  DD
        set ddt_FieldValue = D.[Value]
        from DocumentData DD 
		  inner join Data D on DD.DocumentId = D.ID and DD.ddt_FieldName = D.[Key];

Open in new window


The xml looks like this:

<BRUM-registret><FormidableTest><Id>11</Id><RegisteringUnitId>1</RegisteringUnitId><PersonId>1311</PersonId><PersonIdentity>199107153562</PersonIdentity><UpdateUserId>1</UpdateUserId><CreateDateTime>2014-10-01 11:02:07</CreateDateTime><UpdateDateTime>10/01/2014 11:05:46</UpdateDateTime><Approved>false</Approved><ApprovedDateTime/><ApprovedUserId/><Namn>5567</Namn><Alder>awd</Alder></FormidableTest></BRUM-registret>

Open in new window

0
Comment
Question by:soozh
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
please check the MERGE statement to update or insert as needed:
http://msdn.microsoft.com/en-us/library/bb510625.aspx
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now