Solved

Some TSQL

Posted on 2014-07-31
3
150 Views
Last Modified: 2014-08-14
I have an xml document that is passed into a stored procedure as a parameter called @xmldata.

It can look like this.  Basically a name/value pair.
<BRUM-registret>
  <TestForm1>
    <DocumentId>6</DocumentId>
    <text1>Hello.</text1>
    <text2>Goodbye</text2>
    <text3>Page 2.</text3>
  </TestForm1>
</BRUM-registret>

Open in new window

I need to traverse the document and basically update a table that holds the name/value pairs

So in the case above I need three updates to do the following:

Update MyTable set FieldValue = ‘Hello’ where DocumentId=6 and FieldName = ‘text1’
Update MyTable set FieldValue = ‘Goodbye’ where DocumentId=6 and FieldName = ‘text2’
Update MyTable set FieldValue = ‘Page 2.’ where DocumentId=6 and FieldName = ‘text3’

You can see the DocumentId is a “special name/value pair” because it is used as part of the key to update the other records.

Can anyone suggest some generic code that will parse the XML, extract the values, and update the table?
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
  • 2
3 Comments
 
LVL 33

Assisted Solution

by:ste5an
ste5an earned 50 total points
ID: 40231154
E.g.
DECLARE @XmlData XML;
SET @XmlData = N'
<BRUM-registret>
	<TestForm1>
		<DocumentId>6</DocumentId>
		<text1>Hello.</text1>
		<text2>Goodbye</text2>
		<text3>Page 2.</text3>
	</TestForm1>
</BRUM-registret>
';

SELECT  TestForm1.value('DocumentId[1]', 'INT') AS DocumentId ,
        TestForm1.value('text1[1]', 'NVARCHAR(255)') AS Text1 ,
        TestForm1.value('text2[1]', 'NVARCHAR(255)') AS Text2 ,
        TestForm1.value('text3[1]', 'NVARCHAR(255)') AS Text3
FROM    @XmlData.nodes('/BRUM-registret/TestForm1') A ( TestForm1 );

Open in new window


btw, when it's possible to redefine the XML structure, then you should do it. E.g.
DECLARE @XmlData XML;
SET @XmlData = N'
<BRUM-registret>
	<TestForm1>
		<Document Id="6">
			<Text Line="1">Hello.</Text>
			<Text Line="2">Goodbye</Text>
			<Text Line="3">Page 2.</Text>
		</Document>
	</TestForm1>
</BRUM-registret>
';

SELECT  Document.value('@Id', 'INT') AS DocumentId ,
        [Text].value('@Line', 'NVARCHAR(255)') AS TextLine ,
        [Text].value('.', 'NVARCHAR(255)') AS TextContent
FROM    @XmlData.nodes('/BRUM-registret/TestForm1/Document') A ( Document )
        CROSS APPLY Document.nodes('Text') B ( [Text] );

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40231171
I would say this is the starter:
declare @xml xml
set @xml = '<BRUM-registret>
  <TestForm1>
    <DocumentId>6</DocumentId>
    <text1>Hello.</text1>
    <text2>Goodbye</text2>
    <text3>Page 2.</text3>
  </TestForm1>
</BRUM-registret>'

select n.f.query('fn:local-name(.)') name
 , n.f.value('.', 'nvarchar(max)') value
 , n.f.query('.') xml
  from ( select @xml data ) t
  cross apply t.data.nodes('//TestForm1/*') as n(f)

Open in new window


which returns this:
name	value	xml
DocumentId	6	<DocumentId>6</DocumentId>
text1	Hello.	<text1>Hello.</text1>
text2	Goodbye	<text2>Goodbye</text2>
text3	Page 2.	<text3>Page 2.</text3>

Open in new window

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 450 total points
ID: 40231175
full updated script:
declare @xml xml
set @xml = '<BRUM-registret>
  <TestForm1>
    <DocumentId>6</DocumentId>
    <text1>Hello.</text1>
    <text2>Goodbye</text2>
    <text3>Page 2.</text3>
  </TestForm1>
</BRUM-registret>'


;with x as (
select n.f.value('fn:local-name(.)', 'nvarchar(max)') name
 , n.f.value('.', 'nvarchar(max)') value
 , d.f.value('.', 'nvarchar(max)') docid
 --, n.f.query('.') xml
  from ( select @xml data ) t
  cross apply t.data.nodes('//TestForm1/*') as n(f)
  cross apply t.data.nodes('//TestForm1/DocumentId') as d(f)
  where n.f.value('fn:local-name(.)', 'nvarchar(max)')  <> 'DocumentId'
  )
  update t  
     set FiendValue = x.value
   from MyTable t
   join x 
     on x.docid = t.DocumentID
     and x.name = t.FieldnName

Open in new window

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this article I will describe the Detach & Attach 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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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