Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

tsql - How to continue executing rest of while loop even if an exception occurs in sybase?

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.

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try putting the content inside your while block inside try cactch.

NOTE: The below sample is in SQL, try similar code in 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.
 `

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...