• C.K.Shaiju (9/24/2013)


    Hi,

    How do we ignore an error in T-SQL?

    For e.g.:- The following code throw an error once the @lCounter reach at 15 and will come out. Requirement is it should go till 1000. Even if there are errors in between.

    DECLARE @lCounter SMALLINT

    DECLARE @lError SMALLINT

    SET @lCounter = 0

    WHILE (@lCounter <= 1000 )

    BEGIN

    SELECT CONVERT(VARCHAR, GETDATE(), @lCounter)

    SET @lCounter = @lCounter + 1

    END

    Thanks in advance

    I can't think of any reason why you'd want to do that. . . but something like this: -

    DECLARE @lCounter SMALLINT, @lError SMALLINT;

    SET @lCounter = 0;

    WHILE (@lCounter <= 1000 )

    BEGIN;

    BEGIN TRY;

    SELECT CONVERT(VARCHAR, GETDATE(), @lCounter);

    SET @lCounter = @lCounter + 1;

    END TRY

    BEGIN CATCH;

    SET @lCounter = @lCounter + 1;

    IF @lCounter >= 1000

    BEGIN;

    RAISERROR('Error',16,1);

    BREAK;

    END;

    END CATCH;

    END;


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/