2013-10-15 33 views
0

这与this question有关,但略有不同,我有while循环插入记录,我希望它继续,即使某些插入失败。因此,insertrecords过程插入记录,方法是一次对顶部50行的临时表执行一次操作。即使sybase中发生异常,如何继续执行while循环的其余部分?

问题是如果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 

回答

-1

尝试把你的内容放在你的while块内尝试仙人掌。

NOTE:以下示例在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. 
` 
+1

我不认为'try'是sybase中的有效关键字。 – Vishal

+0

你的示例代码看起来像SQL,并且我给出了一个sql示例。 不确定它会在sybase中工作与否。 – sudhAnsu63

+0

是的,但我提到了sybase,我知道这将在SQL Server中起作用。我在寻找类似于Sybase的东西。 – Vishal