• You are looking for the BREAK keyword.

    a simple example to break out of what would otherwise be an endless loop:

    --RAISERROR immediate message

    --print error immediately in batch

    DECLARE

    @i INT,

    @err VARCHAR(100)

    --set @i=1

    WHILE 0 = 0

    BEGIN

    SET @err = 'Progress So Far: Step ' + CONVERT(VARCHAR(30), ISNULL(@i, 1)) + ' completed.'

    RAISERROR (@err,

    0,

    1) WITH nowait

    WAITFOR delay '00:00:02'

    SET @i=ISNULL(@i, 1) + 1

    IF @i > 5

    BREAK;

    END

    PRINT 'We Broke out of the loop!'

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!