批量插入参数化存储过程 SQL 15
Bulk Insert parameterized stored proc SQL 15
我肯定遗漏了一些愚蠢的东西,但我似乎无法在很多小时后弄明白。
在 SQL 15 中创建了一个存储过程,如下所示:
CREATE PROCEDURE [dbo].[BulkLoad]
@BulkInsertFromFile varchar(255) = null,
@MaxNumberOfFileErrors int = 0
AS
BEGIN
DECLARE @statement NVARCHAR(4000)
DECLARE @parameterDefinition NVARCHAR(4000)
SET @parameterDefinition = N'@a varchar, @b int, @c varchar, @d varchar, @e int'
SET @statement = '
BULK INSERT CombinedRawData
FROM @a
WITH (
FIRSTROW=@b,
FIELDTERMINATOR=@c,
ROWTERMINATOR=@d,
MAXERRORS=@e)'
EXECUTE sp_executesql @statement, @parameterDefinition, @BulkInsertFromFile, 2, ',', '\n', @MaxNumberOfFileErrors
存储过程创建无误。但是,执行使用
EXEC BulkLoad N'C:\Temp\Data.txt',5
我收到错误信息:
“@a”附近的语法不正确。
任何正确方向的帮助或指示将不胜感激。
干杯,
基因
CREATE PROCEDURE [dbo].[BulkLoad]
@BulkInsertFromFile varchar(255) = null,
@MaxNumberOfFileErrors int = 0
AS
BEGIN
DECLARE @statement NVARCHAR(4000)
DECLARE @parameterDefinition NVARCHAR(4000)
--check if file exists..better use something else than xp_fileexist
declare @fileexists int;
exec xp_fileexist @BulkInsertFromFile, @fileexists output;
if isnull(@fileexists, 0) <> 1
begin
raiserror('file does not exist', 16, 1);
return;
end
select @BulkInsertFromFile = replace(@BulkInsertFromFile, '''', '''''');
declare @a varchar(5),
@b int = 2,
@c varchar(5) = ',',
@d varchar(5) = '\n',
@e int = case when isnull(@MaxNumberOfFileErrors, 0) <= 0 then 0 else @MaxNumberOfFileErrors end;
SET @statement = N'
BULK INSERT CombinedRawData
FROM ''' + @BulkInsertFromFile + N'''
WITH (
FIRSTROW= ' + cast(@b as nvarchar(5)) + N',
FIELDTERMINATOR=''' + @c + N''',
ROWTERMINATOR=''' + @d + N''',
MAXERRORS=' + cast(@e as nvarchar(15)) + N'
)';
--print @statement
exec (@statement);
END
我肯定遗漏了一些愚蠢的东西,但我似乎无法在很多小时后弄明白。
在 SQL 15 中创建了一个存储过程,如下所示:
CREATE PROCEDURE [dbo].[BulkLoad]
@BulkInsertFromFile varchar(255) = null,
@MaxNumberOfFileErrors int = 0
AS
BEGIN
DECLARE @statement NVARCHAR(4000)
DECLARE @parameterDefinition NVARCHAR(4000)
SET @parameterDefinition = N'@a varchar, @b int, @c varchar, @d varchar, @e int'
SET @statement = '
BULK INSERT CombinedRawData
FROM @a
WITH (
FIRSTROW=@b,
FIELDTERMINATOR=@c,
ROWTERMINATOR=@d,
MAXERRORS=@e)'
EXECUTE sp_executesql @statement, @parameterDefinition, @BulkInsertFromFile, 2, ',', '\n', @MaxNumberOfFileErrors
存储过程创建无误。但是,执行使用
EXEC BulkLoad N'C:\Temp\Data.txt',5
我收到错误信息: “@a”附近的语法不正确。
任何正确方向的帮助或指示将不胜感激。
干杯, 基因
CREATE PROCEDURE [dbo].[BulkLoad]
@BulkInsertFromFile varchar(255) = null,
@MaxNumberOfFileErrors int = 0
AS
BEGIN
DECLARE @statement NVARCHAR(4000)
DECLARE @parameterDefinition NVARCHAR(4000)
--check if file exists..better use something else than xp_fileexist
declare @fileexists int;
exec xp_fileexist @BulkInsertFromFile, @fileexists output;
if isnull(@fileexists, 0) <> 1
begin
raiserror('file does not exist', 16, 1);
return;
end
select @BulkInsertFromFile = replace(@BulkInsertFromFile, '''', '''''');
declare @a varchar(5),
@b int = 2,
@c varchar(5) = ',',
@d varchar(5) = '\n',
@e int = case when isnull(@MaxNumberOfFileErrors, 0) <= 0 then 0 else @MaxNumberOfFileErrors end;
SET @statement = N'
BULK INSERT CombinedRawData
FROM ''' + @BulkInsertFromFile + N'''
WITH (
FIRSTROW= ' + cast(@b as nvarchar(5)) + N',
FIELDTERMINATOR=''' + @c + N''',
ROWTERMINATOR=''' + @d + N''',
MAXERRORS=' + cast(@e as nvarchar(15)) + N'
)';
--print @statement
exec (@statement);
END