Solved

Update/INSERT data based on an XML?

Posted on 2014-10-01
1
121 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
ID: 40354215
please check the MERGE statement to update or insert as needed:
http://msdn.microsoft.com/en-us/library/bb510625.aspx
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

948 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

22 Experts available now in Live!

Get 1:1 Help Now