2012-02-01 58 views
1

我在做一个批处理。在我的过程中,我构建了一组插入查询语句,并将从我的应用程序运行。我不会使用sql事务并且想要跳过引发错误的语句。sql server跳过一次插入多行的错误

例如:

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 

是否有可能这样类型的方法?

回答

0

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

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

这样你不能,除非你做的

  • 一个由行
  • 提交排包装每个插入一个try/catch

bcp和BULK INSERT有一个MAXERRORS选项,这是没有暴露在.net SQLBulkCopy类,这可能是更好的方式来做到这一点...