即使sybase中发生异常,如何继续执行while循环的其余部分? [英] How to continue executing rest of while loop even if an exception occurs in sybase?
问题描述
这与此问题有关,但略有不同,我有while循环可插入记录而且我希望即使某些插入操作失败,它也能继续执行.因此,insertrecords过程通过在临时表上一次对前50行执行一次插入来插入记录.
This is related to this question but slightly different, I have while loop that inserts records and I want it to continue even if some inserts fail. So, the insertrecords procedure inserts records, by doing a where on the temp table for top 50 rows at a time.
问题是,如果insertrecords中的任何插入失败,它将不会继续?即使当前50条记录失败,如何修改sql以继续下50行.我猜想在sybase中有类似try/catch异常处理的东西吗?
The problem is that it won't continue if any of the inserts inside the insertrecords fail? How can I modify the sql to continue with the next 50 rows, even if it fails for current 50 records. I guess is there something like try/catch exception handling in sybase?
SELECT id INTO #temp FROM myTable
-- Loop through the rows of the temp table
WHILE EXISTS(SELECT 1 FROM #temp)
BEGIN
BEGIN TRANSACTION
exec insertrecords
IF @@error = 0
begin
print 'commited'
commit
end
else
begin
print 'rolled back'
rollback
end
DELETE TOP 50 FROM #temp order by id
END
-- Drop the temp table.
DROP TABLE #temp
推荐答案
尝试将内容放入try仙人掌中的while块中.
Try putting the content inside your while block inside try cactch.
注意: 下面的示例在SQL中,请在sybase中尝试类似的代码.
`WHILE(SOME CONDITION)
BEGIN --start of while block
BEGIN TRY-start of try block
--your code here
END TRY
BEGIN CATCH
PRINT ERR_MESSAGE();
END CATCH
END --end of while loop.
`
这篇关于即使sybase中发生异常,如何继续执行while循环的其余部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!