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

时间:2012-02-01 11:27:46

标签: sql-server

我正在进行批处理。在我的过程中,我构建了一组插入查询语句,并将从我的应用程序运行。我不会使用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

这种过程是否可能?

3 个答案:

答案 0 :(得分:1)

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

  • 逐行提交
  • 将每个INSERT包装在TRY / CATCH

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

答案 1 :(得分:0)

try catch block中包装每个插入语句可以解决问题。

答案 2 :(得分:0)

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

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