SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


BCP Export to CSV w/Column Headers


BCP Export to CSV w/Column Headers

Author
Message
MostInterestingMan
MostInterestingMan
Mr or Mrs. 500
Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)

Group: General Forum Members
Points: 521 Visits: 601
Hi,

Is it possible to include column headers with BCP? Below is my BCP call except that the out file doesn't include headers. I have searched the web and can't find anything to point me in the right direction.

DECLARE @bcpCmd VARCHAR(8000)
SET @bcpCmd = 'bcp "select * from mydatabase.dbo.mytable" queryout f:/test.csv -c -t, -r -T -S myserver'
EXEC master..xp_cmdshell @bcpCmd

NOTE: The server I'm working with in SQL 2000 therefore I can't use SQLCMD. Also the solution have to be done programmably so using SSMS studio to export and save to CSV is not what I need.

Thanks

MCTS: BI 2008, MCITP: BI 2008
Stay Thirsty My Friends
Mike John
Mike John
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2852 Visits: 5961
The only way I have managed to achieve this with BCP is:

Create a view
The view should contain
SELECT 'col1name' as col1name, 'col2name' as col2name etc
UNION ALL -- all is important to avoid a sort
SELECT -- all your columns from whatever table

Then use BCP export the view.

This does rely on the fact that the results of the union will have the first row first - which strictly speaking they may not be, but with it constructed as above I have never seen it incorrect.

Add a function round each column to quote qualify if the column contains a quote, comma, NL etc and you have a very fast and really easy way of exporting to csv.

Each column will need to be char or varchar - or cast to the correct type as well otherwise the union will complain about data types.

Hope that helps

Mike



MostInterestingMan
MostInterestingMan
Mr or Mrs. 500
Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)

Group: General Forum Members
Points: 521 Visits: 601
Mike John (12/16/2010)
The only way I have managed to achieve this with BCP is:

Create a view
The view should contain
SELECT 'col1name' as col1name, 'col2name' as col2name etc
UNION ALL -- all is important to avoid a sort
SELECT -- all your columns from whatever table

Then use BCP export the view.

This does rely on the fact that the results of the union will have the first row first - which strictly speaking they may not be, but with it constructed as above I have never seen it incorrect.

Add a function round each column to quote qualify if the column contains a quote, comma, NL etc and you have a very fast and really easy way of exporting to csv.

Each column will need to be char or varchar - or cast to the correct type as well otherwise the union will complain about data types.

Hope that helps

Mike



Thanks Mike. Your input pointed me in the right direction. Basically what I did was create two files and merge them. I created the bcp query results file without headers first and created another file will headers only using INFORMATION.SCHEMA. Thanks for your input - appreciated.

MCTS: BI 2008, MCITP: BI 2008
Stay Thirsty My Friends
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search