OSQL: Storing result of a Stored Procedure in a file

By Bill Graziano on 17 October 2000 | 3 Comments | Tags: Application Design


Amit writes "What is the exact syntax for storing output of a Stored Procedure or a query in a text file. I was trying bcp utility, but is giving error. Thanks in anticipation"

BCP works ok if you want to store the contents of a table to text file. If you want the contents of a stored procedure it gets complicated. In summary, you must use the stored procedure to fill a table, then use BCP to export the table, and finally you need to clear the table. I wish there was a better way!

Wait. There is and it's called OSQL. OSQL is a command line program that allows you to run SQL Statements. Let's look at the simplest form:

osql /U sa /P password /d pubs /S Server9 /Q "Select * from Authors"

Remember you are typing this into the operating system (command prompt) and NOT Query Analyzer. This will connect to the pubs database on Server9 and run the query Select * from Authors and output the results back to stdout (which is the screen unless you redirect it). And the arguement headers (-U, -P, etc.) are case sensitive. Go figure.

You wanted to get the result of a stored procedure. You can do that like this:

osql /U sa /P password /d pubs /S Server9 /Q "sp_help"

Now we need to save this in a file. The simplest way is like this:

osql /U sa /P password /d pubs /S Server9 /Q "sp_help" -o ofile.txt

This will store the results in a file called ofile.txt. You could store them in a Unicode file using -u ofile.txt. You can also embed the path information in the file name using quotes.

There are a couple of other cool things you can do with this. It's really easy to put the SQL statement you want to run in an environment variable using a batch file. You batch file will look something like this:

set mysqlcommand=sp_help
osql /U sa /P password /d pubs /S Server9 /Q "%mysqlcommand%"


This makes it pretty easy to build a batch system. You can also capture the SQL Server result code at the operating system level using the -b flag.

Other popular settings include disabling headers, setting the row width, using trusted connections and input redirection. With input redirection you can put a series of SQL commands into a file and execute them. Books Online has quite a bit more detail on OSQL if you're so inclined.

Discuss this article: 3 Comments so far. Print this Article.

If you like this article you can sign up for our weekly newsletter. There's an opt-out link at the bottom of each newsletter so it's easy to unsubscribe at any time.

Email Address:

Related Articles

Application Locks (or Mutexes) in SQL Server 2005 (7 January 2008)

What I Wish Developers Knew About SQL Server (Presentation) (11 October 2007)

Multiple Active Result Sets (MARS) – Transactions and Debugging (17 June 2007)

Multiple Active Result Sets (MARS) (3 April 2007)

How SQL Server 2005 Enables Service-Oriented Database Architectures (8 September 2006)

Presentation: What I Wish Developers Knew About SQL Server (17 November 2005)

GeoCoding with SQL Server and Google (8 August 2005)

How to Asynchronously Execute a DTS package from ASP or ASP.NET (27 March 2005)

Other Recent Forum Posts

Data Comparision (0 Replies)

Overlapping Date Ranges - Remove dup Permutations (0 Replies)

SQL Server 2008 querying Active Directory (2 Replies)

Dynamic view (2 Replies)

Database Design in Sql Server Related (3 Replies)

restore with recovery by mistake (2 Replies)

Football (soccer) scoring system (3 Replies)

Left Outer Join/multi-part identifier error help (3 Replies)

Subscribe to SQLTeam.com

Weekly SQL Server newsletter with articles, forum posts, and blog posts via email. Subscribers receive our white paper with performance tips for developers.

SQLTeam.com Articles via RSS

SQLTeam.com Weblog via RSS

- Advertisement -