Solved

Some TSQL

Posted on 2014-07-31
3
132 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
  • 2
3 Comments
 
LVL 32

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 142

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 142

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now