Solved

XML Select

Posted on 2011-03-04
2
309 Views
Last Modified: 2012-05-11
I need to do (one of my first) select as XML statements

I need <?xml version="1.0" encoding="UTF-8" ?> at the top
Then the "root tag" is <Document>
Then a <Header> section with hard coded nodes
Then a <Record> section with firstname, lastname etc nodes

Example would be (selecting first and last name from r_users).
<?xml version="1.0" encoding="UTF-8" ?>
<Document>
  <XMLVersion Version="2.00"/>
  <Header>
       <PacketNum>0000000111222555</PacketNum>
        blah...blah..blah...
  </Header>
  <Record>
     <SeqNum>1</SeqNum>
     <firstName>Bill</firstName>
     <lastName>Gates</lastName>
         blah..blah..blah...
  </Record>
</Document>
0
Comment
Question by:lrbrister
2 Comments
 
LVL 15

Accepted Solution

by:
derekkromm earned 500 total points
ID: 35036158
declare @t table (
      id int,
      fname varchar(100)
)

insert into @t select 1,'gates' union select 2,'jones'

declare @xml xml
select @xml = '<xml version="1.0" encoding="UTF-8" />
<Document>
  <XMLVersion Version="2.00"/>
  <Header>
       <PacketNum>0000000111222555</PacketNum>
        blah...blah..blah...
  </Header>'
+ (
select
      id, fname
from
      @t
for xml path('Record')
) + '
</Document>'

select @xml
0
 

Author Closing Comment

by:lrbrister
ID: 35036303
Thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

776 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