Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 March 4th, 2004, 01:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default For XML Explicit

Hello,

I want to create XML explicitly as follows:

<Documents>
  <Document>
    <Name>
    <Path>
  </Document>
  ...
</Documents>

I am working with the query below, as was given me by an example, which I converted to this new syntax:

select 1 as tag,
       NULL as parent,
       Name as [Document!1!Name!element],
       Path as [Document!1!Path!element]
from Documents
order by Name
for xml explicit

Which creates the XML without the Documents root tag. How do I create that? I don't understand "FOR XML" at all.

Thanks,

Brian
__________________
Brian
 
Old March 5th, 2004, 04:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

I assume you're using ADO to get this XML back to your app? If so, you specify the root node as a property of the ADO command object. Here's a snippet of VB code for getting the results of a FOR XML query into a stream object:
Code:
    ' code here to open a connection named "con"

    Set strm = New ADODB.Stream
    strm.Open

    Set cmd = New ADODB.Command
    cmd.ActiveConnection = con
    cmd.CommandText = "stored proc name"
    cmd.CommandType = adCmdStoredProc
    cmd.Properties("Output Stream") = strm ' specify the output stream
    cmd.Properties("XML Root") = "Documents" 'specify the root node here

    ' add any parameters here

    cmd.Execute , Options:=adExecuteStream ' use this option

    MsgBox strm.ReadText ' to access the XML string
If you want to load it into an XML DOM, you may be able to specify a DOM object as the "Output Stream" instead. I've never tried it myself - must give it a go sometime.

hth
Phil
 
Old March 5th, 2004, 05:05 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Or, to encapsulate it all in a stored procedure, add
Code:
SET NOCOUNT ON
SELECT '<Documents>'
before your existing query and
Code:
SELECT '</Documents>'
SET NOCOUNT OFF
after it.

Joe (MVP - xml)
 
Old March 5th, 2004, 04:40 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I'm using VBScript; what's the numerical equivalent to "adExecuteStream"?

Thanks for all of your help,

Brian
 
Old March 6th, 2004, 05:31 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you are not including adovbs and are able to use the contstants then you can find out any value if you have Excel, Word or one of the other Office applications with VBA. Open a fresh document, go to VBA editor, normally Alt+F11, go to tools | references and check box next to Microsoft ActiveX Data Objects, version 2.5 or later. Okay box closed and press F2, in the dropdown change to ADODB and search for your term, alternatively open the immediate window and type
Code:
?adExecuteStream
--

Joe
 
Old March 8th, 2004, 09:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

1024





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL XML Explicit (Output file required) Neal SQL Server 2005 0 August 4th, 2008 03:04 AM
XML EXPLICIT with text column stevemeyns SQL Server 2005 1 February 7th, 2007 01:09 PM
FOR XML Explicit and ASP tonyellard SQL Server ASP 1 April 8th, 2005 10:41 AM
SQL Server For XML Explicit tonyellard Classic ASP Databases 0 November 3rd, 2003 03:03 PM
CDATA Set with For XML Explicit Yehuda SQL Server 2000 0 August 13th, 2003 01:50 PM





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