SQL Server - BULK INSERT 问题编写 T-SQL 过程会引发错误

问题描述 投票:0回答:1

这段代码工作正常,但这不是我想要的

BULK INSERT GeocodedAddresses
FROM N'E:\SQL_Databases\CCSR\GeoCoordinates\Processing\GeocodeResults-4_Processed.csv'
WITH ( FORMAT = 'CSV', FIRSTROW = 2 );

此代码不断抛出错误:

DECLARE @copyfile  NVARCHAR(100)   = N'GeocodeResults-7_Processed.csv';
DECLARE @source_file nvarchar(4000);
DECLARE @Processing_file nvarchar(4000);

SET @source_file = N'E:\SQL_Databases\CCSR\GeoCoordinates\Output\'         + @copyfile; 

SET @Processing_file = N'E:\SQL_Databases\CCSR\GeoCoordinates\Processing\' + @copyfile; 

DECLARE @sql varchar(max);

SET @sql = 'BULK INSERT dbo.GeocodedAddresses FROM ' + QUOTENAME(@Processing_file, '''') WITH ( FORMAT = 'CSV' );

EXEC (@sql);

错误:

消息 319,第 15 层,状态 1,第 108 行
关键字“with”附近的语法不正确。如果此语句是公用表表达式、xmlnamespaces 子句或更改跟踪上下文子句,则前一个语句必须以分号终止。

有人可以帮我找出我做错了什么吗?

sql-server t-sql bulkinsert
1个回答
0
投票

只是跟进评论(我为你修复了该字符串):

Declare @sql nvarchar(MAX)
declare @Processing_file nvarchar(100) = 'c:\1\file.csv'
set @sql = 'BULK INSERT dbo.GeocodedAddresses FROM ' + QUOTENAME(@Processing_file, '''') +' WITH ( FORMAT = ''CSV'' );'
print @sql

结果将是:

BULK INSERT dbo.GeocodedAddresses FROM 'c:\1\file.csv' WITH ( FORMAT = 'CSV' );
© www.soinside.com 2019 - 2024. All rights reserved.