Solved

Update/INSERT data based on an XML?

Posted on 2014-10-01
1
122 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query 26 61
SSRS 2012 r2 - Parm Drop Down has Date/Time 12 31
Find results from sql within a time span 11 29
MS SQL BCP Extra Lines Between Records 2 14
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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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