用于创建CSV文件的BCP语法

时间:2017-02-08 16:48:04

标签: sql-server bcp

当我尝试运行此程序时,它只是给了我BCP可用的开关。这是我第一次使用BCP。我正在尝试编写存储过程以将一天的数据导出为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

这是返回Return

的内容

由于

1 个答案:

答案 0 :(得分:1)

BCP很奇怪。将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 命令保持在单行

{{1}}