Solved

Some TSQL

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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