jwhitf4770
jwhitf4770

Reputation: 21

BCP syntax to create CSV file

When I try to run this procedure, it just give gives me the switches available to BCP. This is my first time with BCP. I'm trying to write a stored procedure to export one day of data to a CSV.

declare @startdate as datetime = '2017-01-24'
declare @enddate as datetime = dateadd(day,1,@startdate)

declare @sql varchar(8000)

set @sql =
'bcp "select * from tblBOJEOJ
where system = ''MKEV03''
and [date] between ''' + cast(@startdate as nvarchar(11)) + ''' and ''' + cast(@enddate as nvarchar(11)) + ''' " 
queryout D:\Temp\Galaxy\BOJEOJ_.csv
-c -t, -T -S ' + @@SERVERNAME 

print @sql

exec master..xp_cmdshell @sql

This is what returnsReturn

Thanks

Upvotes: 0

Views: 6026

Answers (1)

Pரதீப்
Pரதீப்

Reputation: 93694

BCP is quite strange. Keep the BCP command in single line

declare @sql varchar(8000)

set @sql =
'bcp "select * from tblBOJEOJ where system = ''MKEV03'' and [date] between ''' + cast(@startdate as nvarchar(11)) + ''' and ''' + cast(@enddate as nvarchar(11)) + ''' " queryout D:\Temp\Galaxy\BOJEOJ_.csv -c -t, -T -S ' + @@SERVERNAME 

print @sql

exec master..xp_cmdshell @sql

Upvotes: 1

Related Questions