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 April 23rd, 2004, 12:27 PM
Authorized User
 
Join Date: Apr 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default objCmd.Execute error

Trying to extract the results of an sql query to a file, an error is produced when the vb script reaches "objCmd.Execute". Is the number/type of arguments that cause that error?!

Best Regards,
Theodore.

The code follows:

Set objConn = CreateObject("ADODB.Connection")
objConn.Open("PROVIDER=SQLOLEDB;SERVER=TestServer; DATABASE=TestDB;UID=sa;PWD=;")

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

objCmd.CommandText = "select * from Market FOR XML AUTO, ELEMENTS"
objCmd.CommandType = 4

set objStream = CreateObject("ADODB.Stream")
objStream.Open
objCmd.Properties("Output Stream") = objStream
strXML = objStream.ReadText

objCmd.Execute , , 1024 'adExecuteStream

objConn.Close
Set objConn = Nothing


 
Old April 26th, 2004, 03:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

You don't say what the error is, so all I can do is point out the few things that look strange to me:
1. the items SERVER and DATABASE in your connection string are usually referred to as Data Source and Initial Catalog
2. CommandType 4 is "stored procedure", but you are executing a SQL string so you want CommandType 1.
3. strXML = objStream.ReadText : shouldn't this line be *after* the SQL has been executed?

Finally, this property, objCmd.Properties("XML Root") = "whatever" is useful too because you don't get a root element by default with FOR XML queries.

hth
Phil
 
Old April 26th, 2004, 03:43 AM
Authorized User
 
Join Date: Apr 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the advice, I changed CommandType to 1 and worked but seems that there is a problem getting over the string copy&paste with the strXML.
I have changed the way I do this with the following:


Set objConn = CreateObject("ADODB.Connection")
objConn.Open("PROVIDER=SQLOLEDB;SERVER=TestServer; DATABASE=TestDB;UID=sa;PWD=;")

Set rsTitles = CreateObject("ADODB.Recordset")
rsTitles.Open "select * from Market FOR XML AUTO, ELEMENTS", objConn

szFile = "F:\thodoris\TestFile.xml"
rsTitles.Save szFile, adPersistXML

rsTitles.Close
objConn.Close
Set objConn = Nothing


But the format of the xml file I am getting does not look at all like xml! Do you have any idea what might go wrong?!

Best Regards,
Theodore.

Quote:
quote:Originally posted by pgtips
 You don't say what the error is, so all I can do is point out the few things that look strange to me:
1. the items SERVER and DATABASE in your connection string are usually referred to as Data Source and Initial Catalog
2. CommandType 4 is "stored procedure", but you are executing a SQL string so you want CommandType 1.
3. strXML = objStream.ReadText : shouldn't this line be *after* the SQL has been executed?

Finally, this property, objCmd.Properties("XML Root") = "whatever" is useful too because you don't get a root element by default with FOR XML queries.

hth
Phil
 
Old April 26th, 2004, 04:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes, all your XML is being escaped because you are saving an XML string using the adPersistXML parameter.

You should either:
1. execute a "standard" , i.e. not "FOR XML", query and then use the adPersistXML method to get it into XML format in a file, OR
2. use your previous method of executing a "FOR XML" query to an ADO Stream, then use the Stream's SaveToFile method to get it into a file.

You can't mix these methods or you end up with an ugly mess.

hth
Phil
 
Old April 26th, 2004, 07:20 AM
Authorized User
 
Join Date: Apr 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank Phil,
I followed the first way and the xml formatted file was created.
The code follows:

Set objConn = CreateObject("ADODB.Connection")
objConn.Open("PROVIDER=SQLOLEDB;SERVER=TestServer; DATABASE=TestDB;UID=sa;PWD=;")

Set rsTitles = CreateObject("ADODB.Recordset")
rsTitles.Open "select * from Market", objConn

szFile = "F:\thodoris\TestFile.xml"
rsTitles.Save szFile, 1 'adPersistXML


Regards,
Theodore.


Quote:
quote:Originally posted by pgtips
 Yes, all your XML is being escaped because you are saving an XML string using the adPersistXML parameter.

You should either:
1. execute a "standard" , i.e. not "FOR XML", query and then use the adPersistXML method to get it into XML format in a file, OR
2. use your previous method of executing a "FOR XML" query to an ADO Stream, then use the Stream's SaveToFile method to get it into a file.

You can't mix these methods or you end up with an ugly mess.

hth
Phil






Similar Threads
Thread Thread Starter Forum Replies Last Post
objCmd.Execute error nnrin Classic ASP Databases 13 October 4th, 2007 02:25 PM
Intermittent Execute Error Lolly Oracle ASP 22 March 11th, 2006 04:21 PM
Execute Package Error dgosche BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 2 February 13th, 2006 12:14 PM
Server.Execute() Error handling nickelsberry Classic ASP Professional 2 April 14th, 2004 04:45 PM
Execute DTS on Scheduled Error Jane SQL Server DTS 5 November 21st, 2003 05:20 AM





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