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