Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 24th, 2006, 02:16 AM
Authorized User
 
Join Date: Dec 2005
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to kapi.goel
Default SQL Table to XML Document

Hi Experts,

Can anybody tell me the code for converting the table
records to a XML document.

I want to write a procedure which will return a string
( varchar(8000) )
containing the xml document.

The Sample XML document i need in the form of string is:

<QATollFreeLocations>
    <LocationName>USA</LocationName>
    <LocationNumber>123456453478</LocationNumber>
</QATollFreeLocations>


Please guide me with an example

Regards,

KG
__________________
KG
 
Old April 24th, 2006, 02:29 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

As you don't show the table structure it's hard to say but normally I would use the FOR XML method.
Search BOL for "FOR XML". If you are using SQL 2005 then I suggest you concentrate on "FOR XML PATH" which is the newer easier way to return custom structures.

--

Joe (Microsoft MVP - XML)
 
Old April 24th, 2006, 03:52 AM
Authorized User
 
Join Date: Dec 2005
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to kapi.goel
Default

I am already using "FOR XML"

SELECT LocationName, LocationNumber
FROM QATollFreeLocations
WHERE QAId = 775904
FOR XML Auto, Elements

while using Elements keyword the result i am getting is incomplete.

but how will i store the result into a string variable of type varcha

Regards,

KG
 
Old April 24th, 2006, 05:07 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I'm not sure what you mean by incomplete, please show what you mean.
If you are using SQL 2000 then you cannot do anything other than return the results of the query.
If you are using SQL 2005 then you can assign the results as with other variables:
Code:
DECLARE @LocationsXml XML
SET @LocationsXml = (SELECT LocationName, LocationNumber
FROM QATollFreeLocations
WHERE QAId = 775904
FOR XML Auto, Elements)

--

Joe (Microsoft MVP - XML)
 
Old April 24th, 2006, 08:03 AM
Authorized User
 
Join Date: Dec 2005
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to kapi.goel
Default

I think this could be the solution, wch we can use using sql
server 2000.

//template.tpl file

<root>
<%begindetail%>
<%insert_data_here%>
<%enddetail%>
</root>

//Inbuilt procedure in sql server 2000
exec sp_makewebtask
@outputfile = 'c:\temp\myxmlfile.xml',
@query = 'select * from sysobjects for xml auto',
@templatefile = 'c:\temp\template.tpl'

but here problem i am facing is

"SQL Web Assistant: Could not open the template file"

Can any body help me in resolving this..

Regards,


KG





Similar Threads
Thread Thread Starter Forum Replies Last Post
XMl Document Reading in SQL Server shashideore SQL Server 2005 1 April 26th, 2007 07:16 AM
create 2-columns table from an XML document frosty1575 XSLT 2 May 16th, 2006 04:17 AM
Write Nested XML document into SQL Server tables boondocksaint20 VB.NET 0 May 2nd, 2006 12:35 PM
SQL Table to XML Document kapi.goel SQL Language 1 April 27th, 2006 07:24 AM
SQL Table to XML Document kapi.goel SQL Server 2000 5 April 25th, 2006 06:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.