Solved

Update/INSERT data based on an XML?

Posted on 2014-10-01
1
127 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 143

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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