0

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

3
  • Which dbms are you using? (That code is product specific.) Commented Feb 8, 2017 at 16:48
  • Can you include the result from your PRINT statement in the question? Commented Feb 8, 2017 at 16:49
  • The single line BCP below fixed it. Commented Feb 8, 2017 at 21:03

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks this fixed it! I did have to add dbname.dbo to my table in the select.
@jwhitf4770 - Instead you can use -d parameter
Is there a switch to put column names on the file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.