?
Solved

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

Posted on 2011-02-24
2
Medium Priority
?
4,643 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
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.

741 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