• subscribe
January 24, 2002 12:00 AM

Using bcp to Copy a Result Set to a File

SQL Server Pro
InstantDoc ID #23502

Can I use bulk copy program (bcp) to copy the result set of a stored procedure to a file?

Absolutely. Imagine that you've defined a procedure in Northwind called BcpOutOrders. The following command copies the data to a text file called authors.txt:

Bcp "EXEC northwind..BcpOutOrders"
QUERYOUT authors.txt —Usa —Ppassword
—Sserver —c

The trick is using the QUERYOUT keyword rather than the usual OUT keyword that you use when you reference a table or view. To avoid frustration, keep two things in mind. First, if the procedure returns multiple result sets, the QUERYOUT statement copies only the first result set to the data file. Second, the bcp utility is particular about how you use quotation marks. SQL Server Books Online (BOL) contains details about how to punctuate your code; you need to use double quotes around a query or procedure name and single quotes for items embedded in the query.



ARTICLE TOOLS

Comments
  • John
    4 years ago
    Jul 15, 2008

    The links in your article take you to a page on MySQL and another Ad by HP. Kinda tacky don't you agree?

  • Anonymous User
    7 years ago
    Jan 24, 2005

    When you want to use it in T-SQL to export using EXEC xpcmdshell bcp ...

  • Anonymous User
    8 years ago
    Oct 31, 2004

    DIDN't GET EVAN A LITTLE HELP

  • Randall Petty
    10 years ago
    Feb 01, 2002

    Great Tip. However, the DBA I work for is wondering why this would be used instead of DTS ( except for the possibility of better performance with BCP ). ??
    Randy

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...