Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 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 Answer

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.
 `

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...