2012-02-01 45 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類,這可能是更好的方式來做到這一點...