Solved

Some TSQL

Posted on 2014-07-31
3
163 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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 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