这与this question有关但略有不同,我有while循环插入记录,我希望它继续,即使某些插入失败。因此,insertrecords过程通过在临时表上一次执行前50行的位置来插入记录。
问题是如果insertrecords中的任何插入失败,它将无法继续?如何修改sql以继续接下来的50行,即使当前50条记录失败也是如此。我猜有没有像sybase中的try / catch异常处理?
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
答案 0 :(得分:-1)
尝试将内容放在try块内的while块中。
注意: 下面的示例是在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.
`