 |
| 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
|
|
|
|

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

March 8th, 2004, 09:27 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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.
|
|

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

March 8th, 2004, 10:02 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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.
|
|

March 8th, 2004, 10:43 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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?
|
|

March 8th, 2004, 10:50 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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.
|
|

March 8th, 2004, 11:20 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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
|
|

March 8th, 2004, 11:24 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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?
|
|

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

March 8th, 2004, 03:04 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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?
|
|
 |