?
Solved

BCP commnad copy data to file

Posted on 2010-08-24
4
Medium Priority
?
505 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 2000 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
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: …

765 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