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 8th, 2004, 09:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default write result set

Hello,

Is there a stored procedure in SQL Server that will write a result set returned from a select statement and write it to a file on a server?

Thanks,

Brian Mains
__________________
Brian
 
Old March 8th, 2004, 09:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Do you want it in text format? Do you have a text driver installed (e.g Microsoft Jet)? If so you can use OPENROWSET or OPENDATASOURCE to insert the results into a text file.
 
Old March 8th, 2004, 09:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Yes, I want it in a text format. Thanks.
 
Old March 8th, 2004, 10:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Do I use an insert statement to get the data in there, such as:
Code:
insert into OpenDataSource('Microsoft.Jet.OLEDB.4.0',
  'Data Source="c:\mydoc.xls";User ID=Admin;Password=;Extended properties=Excel 8.0')
...Select statement...
This isn't my example, it is one that I've found. Is that correct?

Thanks.
 
Old March 8th, 2004, 10:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes that's the idea. Here's the syntax you'll need for a text file (this statement inserts three fields from the Northwind categories table into a text file c:\temp\new.txt):
Code:
INSERT OPENROWSET('Microsoft.Jet.OLEDB.4.0',
   'Text;Database=c:\Temp',new#txt)
SELECT CategoryID, CategoryName, [Description]
FROM Categories
One problem I've come across while experimenting with this is that the text file must exist first
I tried a SELECT INTO query to create the text file, like this:
Code:
SELECT CategoryID, CategoryName, [Description]
INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
   'Text;Database=c:\Temp',new#txt)
FROM Categories
but it doesn't like the OPENROWSET there...

Anyone else have any ideas about how to create the text file as well?

Maybe DTS would be a better option?
 
Old March 8th, 2004, 10:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I've looked into it; actually, I should have stated that I'm using the FOR XML EXPLICIT option to return an XML result set. That is what I'm trying to write to a file out on a server. I have the SQL Statement to output the XML in a stored procedure.
 
Old March 8th, 2004, 11:20 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Quote:
quote:Originally posted by bmains
 I've looked into it; actually, I should have stated that I'm using the FOR XML EXPLICIT option to return an XML result set. That is what I'm trying to write to a file out on a server. I have the SQL Statement to output the XML in a stored procedure.
In that case you maybe better off creating a DomDocument using one of the OLE stored procedures and saving the xml that way. OpenRowset only deals with tabular results, not xml structured ones.


--

Joe
 
Old March 8th, 2004, 11:24 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Your options are even more limited then, since you can't use FOR XML in an INSERT statement.

Maybe better to get the XML into an ADO Stream and use the Stream's SaveToFile() method to save it off to disk?
 
Old March 8th, 2004, 01:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Could the OpenXML function be used in conjunction with an insert statement?
 
Old March 8th, 2004, 03:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I created this script to be used in a DTS package:
Code:
set objConn = CreateObject("ADODB.Connection")
objConn.Open(m_strConnectionString)

set objCmd = CreateObject("ADODB.Command")
set objCmd.ActiveConnection = objConn

objCmd.CommandText = "xmlSelectData"
objCmd.CommandType = 4

set objStream = CreateObject("ADODB.Stream")
objStream.Open

objCmd.Properties("Output Stream") = objStream
'adPersistXML
objCmd.Execute , , 1

strXML = objStream.ReadText

'... Close objects
But the file contains no XML when written out to the file. Is this correct?





Similar Threads
Thread Thread Starter Forum Replies Last Post
help - EXSLT func cannot set more than one result! anboss XSLT 1 October 28th, 2008 02:17 PM
getting a result set to a vector cakalanka Java Databases 1 March 24th, 2008 07:33 AM
Please help my code is not giving me a result set yudee JSP Basics 0 September 20th, 2006 09:55 AM
Result set jemacc SQL Server 2000 0 December 19th, 2003 07:26 PM
Using a SP result set in a FROM clause Bernard Omiple SQL Server 2000 1 October 3rd, 2003 05:38 AM





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