Sql server 一次插入多行时发生sql server跳过错误

Sql server 一次插入多行时发生sql server跳过错误,sql-server,Sql Server,我正在做批处理。在我的过程中,我构建了一组insert查询语句,并将从我的应用程序运行。我不会使用sql事务,希望跳过抛出错误的语句 例如: create table test ( test varchar(20) ) insert into test (test) values ('test1'); -- 1 row affected insert into test (test) values ('test2'); -- 1 row affected insert into test (t

我正在做批处理。在我的过程中,我构建了一组insert查询语句,并将从我的应用程序运行。我不会使用sql事务,希望跳过抛出错误的语句

例如:

create table test
(
test varchar(20)
)

insert into test (test) values ('test1'); -- 1 row affected
insert into test (test) values ('test2'); -- 1 row affected
insert into test (test) values ('a' + 333); -- This will throw error while executing and i ---want to skip this
insert into test (test) values ('test4'); -- This should be affected as per my requirement

是否可能采用这种类型的过程?

将每个insert语句包装在中可以解决此问题。

将每个insert语句包装在中可以解决此问题。

您只需在每个insert语句周围加上一个try-catch块,例如

BEGIN TRY
    INSERT INTO test...
END TRY
BEGIN CATCH
     --Do nothing
END CATCH

您可以用try-catch块围绕每个INSERT语句,例如

BEGIN TRY
    INSERT INTO test...
END TRY
BEGIN CATCH
     --Do nothing
END CATCH

像这样你不能,除非你做一个

  • 逐行提交
  • 用TRY/CATCH包装每个插件

bcp和BULK INSERT有一个在.net类中没有公开的选项,这可能是更好的方法…

像这样,您不能,除非您执行以下操作之一

  • 逐行提交
  • 用TRY/CATCH包装每个插件
bcp和BULK INSERT有一个未在.net类中公开的选项,这可能是更好的方法