SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Using COM to Output Queries to a Text File

By Les Smith, 2001/07/20

Total article views: 6164 | Views in the last 30 days: 32

Using COM to Output Queries to a Text File

Purpose of script: Create a tab delimited file for an ASP intranet application. The files are to be text versions of static HTML reports that the user can download when an "Excel file" is needed.

We needed to create a method of creating tab delimited files based on the results of an SQL query. Surprisingly, there seemed to be no system stored procedure within SQL Server that could perform this task.

One possibility was using a DTS package. However, I did not see a way to pass parameters to the package for changing queries and the text file target.

So I decided to create a simple com object to export a query to a file. I used Visual Basic to create the class which references ADO and Scripting Runtime. Again, DTS seemed like a possibility. I could create a DTS com object in Visual Basic. However, DTS seemed really too difficult too code, when a simpler API would work just as well.

Another decision is whether to have a stored procedure or an Active Server Page instantiate and use the com object. Having the stored procedure do it seems to be the better choice for the following reasons:

  1. It keeps the Active Server Page simpler. Stored procedures are the only interface the ASP programmer needs to use. Additional complexity is avoided.
  2. Several web sites could use the same stored procedure. Otherwise, one would need to register the com object on each web site and keep each web server updated with the latest version. Thus deploying and maintenance are simpler.
  3. Com objects instantiated by ASP are annoyingly over persistent. After an ASP page instantiates the object it is never released, and the DLL becomes locked. At the point one needs to stop the web server, and sometimes even reboot the machine. The sp_OAdestroy system stored procedure seems to do a nice job of destroying the object. One might expect that in ASP the command ‘set obj = nothing’ to do the same, but it does not. According to Microsoft this behavior is by design. See http://support.microsoft.com/support/kb/articles/Q200/2/60.ASP
The article mentions this as an issue for Personal Web Server, but this also applies to IIS.

Calling Method: sp_exportData 'sa','sa',"select * from Suppliers", "Suppliers.txt", 'Northwind'

Download:


Return to Les Smith's Home

 

By Les Smith, 2001/07/20

Total article views: 6164 | Views in the last 30 days: 32
Your response
 
 
Related tags

Advanced Querying     T-SQL    
Programming     Visual Basic 6    
 
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com