Solved

BCP commnad copy data to file

Posted on 2010-08-24
4
503 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql Permission 6 74
Upgrading SQL 2005 Express to 2008 R2 Express 31 164
CROSS APPLY 4 62
Need help debbuging stored procedure 21 48
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 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