Solved

BCP commnad copy data to file

Posted on 2010-08-24
4
499 Views
Last Modified: 2012-05-10

I am using BCP command to exporting result set to .csv file. Here is the code,

declare @SQLcommand nvarchar(4000)

set @SQLcommand = 'bcp DATABASENAME.dbo.UBCommitteeReport out filename.csv -T -c -t, -S SERVERNAME'
      
exec xp_cmdshell @SQLcommand

Is there any way to copy the column name/header name into the .csv file using the bcp? If not, what are the other alternatives?

Any help would be greatly appreciated.

Thanks,


0
Comment
Question by:andrishelp
  • 2
4 Comments
 
LVL 12

Expert Comment

by:mcv22
Comment Utility
You cannot copy column headers using BCP. If you have the same table schema at the destination, you can create a format file (that contains source table column datatypes and widths) to export / import the data.

You can create an xml format file as follows:

bcp DATABASENAME.dbo.UBCommitteeReport format nul -f UBCommitteeReport.xml -T -c -t ","

You would then import the data as follows:

bcp TargetDatabase.dbo.TargetTable in filename.csv -T -k -E -b 1000 -f UBCommitteeReport.xml

The data import/export wizard or SSIS will allow you to export column headers to flat files.
0
 
LVL 16

Expert Comment

by:carsRST
Comment Utility
Try SQLCMD utility.

-h  Controls the headers
http://msdn.microsoft.com/en-us/library/ms162773.aspx

Or you could use SSIS to export to CSV
0
 

Author Comment

by:andrishelp
Comment Utility
mcv22:
As you suggested, I have tried

You can create an xml format file as follows:
bcp DATABASENAME.dbo.UBCommitteeReport format nul -f UBCommitteeReport.xml -T -c -t ","

but it did not work so I changed the little bit on that bcp stmt..

declare @SQLcommand nvarchar(500)

set @SQLcommand = 'bcp databasename.dbo.UBCommitteeReport format nul -c -x -f UBCommitteeReport.xml -t, -T -S servername'

exec xp_cmdshell @SQLcommand

It runs without any errors but output is null eventhough "UBCommitteeReport" tbl does have some data.

What would be the targetdatabase?
bcp TargetDatabase.dbo.TargetTable in filename.csv -T -k -E -b 1000 -f UBCommitteeReport.xml


CarsRST: I have tried SQLCMD utility yet but will try sometimes today.

Thanks,
0
 
LVL 12

Accepted Solution

by:
mcv22 earned 500 total points
Comment Utility
The format file contains only the table structure information. It will end up creating an xml file. In addition you will also have to export the csv containing the actual data as you were doing previously.

Target (destination) database is where you will be importing the data from the flat file (if another server, you will also need -S targetservername).
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now