带有批量复制的 CASE 语句,导出为 csv
CASE statement with Bulk copy, export to csv
I am exporting files to .csv using bulk copy utility, I have eight
tables and want to avoid making eight different procedures, How can I
put it all inside this one ?
这是我的尝试...
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
BEGIN
DECLARE @sqlClients varchar(8000)
SELECT @sqlClients =
CASE @table WHEN 'Clients' THEN 'bcp "select
* from ' + DB_NAME() + '.dbo.' + @table + '
where ReportingDate = ''' + @date + '''"' + ' queryout ' + @output + ' -c
-C65001 -t";" -r"\n" -T -S' + @server
WHEN 'Receivables' THEN 'bcp "SELECT * from ' + DB_NAME() + + '.dbo.' +
@table + ' where ReportingDate = ''' + @date + '''"' + ' queryout ' +
@output + ' -c -C65001 -t";" -r"\n" -T -S' + @server
...
END
exec master..xp_cmdshell @sqlClients
-- 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)'
除了客户和应收账款之外,我还有八个 tables,并且 基于主过程调用中提供的 @table 参数我想要不同的bcp select。
我该怎么做?
当我 运行 以上脚本时,我将收到:
该错误是由于在为@sqlClients 赋值时case 语句结束不当造成的。只需在以下语句的末尾添加 'End ' 关键字 -
SELECT @sqlClients =
CASE @table WHEN 'Clients' THEN 'bcp "select
* from ' + DB_NAME() + '.dbo.' + @table + '
where ReportingDate = ''' + @date + '''"' + ' queryout ' + @output + ' -c
-C65001 -t";" -r"\n" -T -S' + @server
WHEN 'Receivables' THEN 'bcp "SELECT * from ' + DB_NAME() + + '.dbo.' +
@table + ' where ReportingDate = ''' + @date + '''"' + ' queryout ' +
@output + ' -c -C65001 -t";" -r"\n" -T -S' + @server end --<<<----MISSING END KEYWORD
I am exporting files to .csv using bulk copy utility, I have eight tables and want to avoid making eight different procedures, How can I put it all inside this one ?
这是我的尝试...
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
BEGIN
DECLARE @sqlClients varchar(8000)
SELECT @sqlClients =
CASE @table WHEN 'Clients' THEN 'bcp "select
* from ' + DB_NAME() + '.dbo.' + @table + '
where ReportingDate = ''' + @date + '''"' + ' queryout ' + @output + ' -c
-C65001 -t";" -r"\n" -T -S' + @server
WHEN 'Receivables' THEN 'bcp "SELECT * from ' + DB_NAME() + + '.dbo.' +
@table + ' where ReportingDate = ''' + @date + '''"' + ' queryout ' +
@output + ' -c -C65001 -t";" -r"\n" -T -S' + @server
...
END
exec master..xp_cmdshell @sqlClients
-- 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)'
除了客户和应收账款之外,我还有八个 tables,并且 基于主过程调用中提供的 @table 参数我想要不同的bcp select。 我该怎么做? 当我 运行 以上脚本时,我将收到:
该错误是由于在为@sqlClients 赋值时case 语句结束不当造成的。只需在以下语句的末尾添加 'End ' 关键字 -
SELECT @sqlClients =
CASE @table WHEN 'Clients' THEN 'bcp "select
* from ' + DB_NAME() + '.dbo.' + @table + '
where ReportingDate = ''' + @date + '''"' + ' queryout ' + @output + ' -c
-C65001 -t";" -r"\n" -T -S' + @server
WHEN 'Receivables' THEN 'bcp "SELECT * from ' + DB_NAME() + + '.dbo.' +
@table + ' where ReportingDate = ''' + @date + '''"' + ' queryout ' +
@output + ' -c -C65001 -t";" -r"\n" -T -S' + @server end --<<<----MISSING END KEYWORD