Solved

BCP commnad copy data to file

Posted on 2010-08-24
4
504 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

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.
In this article I will describe the Copy Database Wizard 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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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