Solved

How do I spool the output of a query to a file in SQL Server

Posted on 2011-02-24
2
4,284 Views
Last Modified: 2013-11-05
Is there an equivalent to Oracle’s “spool” function on SQL Server? I need the output of a query spool to a file. The query will be run as a cron job as a schedule task, any suggestions?    
0
Comment
Question by:dbawant2b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
MarioAlcaide earned 500 total points
ID: 34970828
We have many options.  Here are just a few that I think can help solve your problem...

SQL Server 2000:
OSQL/ISQL - SQL Server 2000 command line tools.
SQL DMO - Management API
T-SQL

SQL Server 2005:
SQLCMD - Command line tool
SQL SMO - Management API (really easy to script in .NET)
Generate Script Wizard (You choose the objects in your database that you want to script and the settings.  It generates the scripts for you.) (In SQL Server 2005 SP2, you can generate DROP statements for objects and script each object to its own file.)


If you want to save the results from this T-SQL script to a file, you can:

1) Have Management Studio save it to a file:
Query | Results To | Results to File or Shortcut Key (CTRL + SHIFT + F)

2) Use SQLCMD to output the results to a file:
a) Save the T-SQL query above to a .sql file
b) Use the -e option to echo your input file into your output file
SQLCMD -i Input.sql -o C:\Results.txt -e

0
 

Author Closing Comment

by:dbawant2b
ID: 34972156
Thanks for your help.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question