【发布时间】:2018-02-05 10:45:59
【问题描述】:
如何在导出的 .csv 文件中忽略空值,我使用了 bcp 实用程序 在 sql server 中,这就是它的样子。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID ('[dbo].[generateCSV]') IS NOT NULL
DROP PROCEDURE [dbo].[generateCSV]
GO
CREATE PROCEDURE [dbo].[generateCSV]
(
@table varchar(100),
@output varchar(100),
@date varchar(12),
@server varchar(30)
)
AS
DECLARE @sql varchar(8000)
SELECT @sql = 'bcp "select * from ' + DB_NAME() + '.dbo.' + @table + '
where reportingdate = ''' + @date + '''"' + ' queryout ' + @output + ' -c
-C65001 -t";" -r"\n" -T -S' + @server
exec master..xp_cmdshell @sql
-- Main EXEC
EXEC dbo.generateCSV @table = 'Clients', @date = '2017-10-31', @output =
'//172.18.16.109/share/Test.csv (server with export target location ) ',
@server = '172.18.16.108(server we are connected to and from which we are
taking the data)'
在 notepad++ 中导出并打开我的文件后,空列最终被 NULL 填充。
我希望它看起来像......
编辑:
我在我的选择子句中包含了 ISNULL(),但记事本仍然会将这些列读取为空值。
SELECT @sql = 'bcp "select
ReportingDate,uniqClientID,registrationNumber,name,ISNULL(vatNumber,'''') as
vatNumber,ISNULL(entityStatusCode,'''') as entityStatusCode,
maximumLifetimeDPD from ' + DB_NAME() + '.dbo.' +
@table + ' where ReportingDate = ''' + @date + '''"' + ' queryout ' +
@output + ' -c -C65001 -t";" -r"\n" -T -S' + @server
【问题讨论】:
-
对空列使用 isnull 函数
-
为什么不使用 SSIS 和数据导入/导出向导?该向导实际上会生成一个您可以重复使用或编辑的 SSIS 包。
bcp是批量工具,不是 CSV 导入/导出工具 -
客户要求程序。
标签: sql-server csv tsql export-to-csv bcp