Solved

BCP commnad copy data to file

Posted on 2010-08-24
4
501 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
ID: 33515223
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
ID: 33515311
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
ID: 33520628
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
ID: 33523578
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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