Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Some TSQL

Posted on 2014-07-31
3
Medium Priority
?
164 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 35

Assisted Solution

by:ste5an
ste5an earned 200 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 1800 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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 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