Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to create headers in csv output file using BCP

Author  Topic 

RJ
Starting Member

11 Posts

Posted - 2011-06-22 : 07:36:29
I use BCP with a format file to export data into a CSV file. This BCP process is incoorperated in a STORED PROCEDURE, which is activated by running a report. However, I require header information to be placed in the CSV file but cannot find anyway of achieving this.

Is there a way of specifying headers in the format file or do I need to add it to the SP script?

Here is a copy of the SP and BCP script.


(
@StartDate Datetime,
@EndDate Datetime,
@Source Int=0

)
AS

Declare @sql varchar(500);

select cust.custno, cust.fname, cust.lname, cust.addr1, cust.addr2, cust.addr2e, cust.addr3, cust.po, cust.postcd,
cust.email, cust.htel, cust.wtel, cust.tel3, cust.consent, cust.nocontact,
vehi.licno, vehi.serialno, vehi.make, vehi.model, vehi.submodel,
convert(varchar,vehi.fregd, 103) as RegDate,
convert(varchar,vehi.nmd, 103) as NextSvcDate,
convert(varchar,vehi.last_service, 103) as LastSvcDate,
convert(varchar,vehi.ninspecday, 103) as NxtInspDate,
convert(varchar,vehi.inspecd, 103) as LastInspDate,
convert(varchar,vehi.next_emission_inspect, 103) as NxtEmiDate,
convert(varchar,vehi.last_emission_inspect, 103) as LastEmiDate
from vehi
join cust on cust.custno = vehi.custno

where nmd between @StartDate and @EndDate

if @Source = 0
begin
select @sql = 'bcp "exec AMDEMSYS.dbo.A001_SVC_DATES '
+ '''' + CONVERT(varchar(8),@StartDate,112) + ''''
+ ',''' + CONVERT(varchar(8),@EndDate,112) + ''''
+ ',1"'
+ ' queryout C:\SVC_Data\SVC.CSV -S cc-rjs-xplaptop\sqlexpress -q -f C:\SVC_Data\SVC.fmt -U sa -P sa'
exec master..xp_cmdshell @sql
end

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-06-29 : 04:30:49
See if you can make use of method 5
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

RJ
Starting Member

11 Posts

Posted - 2011-06-29 : 05:54:40
Thanks, your help is much appreciated.
Go to Top of Page
   

- Advertisement -