Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2008 > SQL Server 2008
|
SQL Server 2008 General discussion of SQL Server *2008* version only - not related to a specific book.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2008 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 21st, 2012, 11:02 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Question How to export SQL data to CSV file with headers using BCP

I created a simple Stored Procedure (SP) that queries a table and renames the column names, and I'm able to push those results out to a CSV file *with* those header names when done manually.

But when I try to export the results to a CSV file by running the same SP using xp_cmdshell with bcp, the header row (Col1, Col2, and Col3) does not appear in the resulting CSV file.

ORIGINAL TABLE:

ColumnName1-----ColumnName2-----ColumnName3
Joe Schmo Customer
Jane Doe Customer
Tim Tiny Musician

QUERY WITHIN SP:

SELECT ColumnName1 AS Col1, ColumnName2 AS Col2, ColumnName3 AS Col3
FROM TABLENAME

QUERY RESULT:

Col1-----Col2-----Col3 <<<<----- WHAT I WANT!
Joe Schmo Customer
Jane Doe Customer
Tiny Tim Musician

Here's the code I'm using within the job:

EXEC xp_cmdshell 'bcp "EXEC DATABASENAME.dbo.STOREDPROCEDURENAME" QUERYOUT "\\MYSERVERNAME\files\export.csv" -c -t, -T -S'

CSV FILE RESULT USING BCP:

Joe Schmo Customer
Jane Doe Customer
Tim Tiny Musician

As you can see the headers are missing. What am I doing wrong?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Export data from sql table to a file Frederico Visual Web Developer 2008 0 March 24th, 2010 02:02 PM
import data from csv/xsl file to sql bex WinForms/Console Application Design 0 July 9th, 2009 02:34 PM
How do I export data from a DataViewGrid to a CSV pauljohns353 VB How-To 1 November 22nd, 2006 11:57 AM
Export To .csv File paul31Hampshire Access VBA 18 June 26th, 2006 11:30 AM
Export data in to csv file from asp.netpage madhusrp ASP.NET 1.0 and 1.1 Professional 1 June 9th, 2006 09:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.