An alternative (simpler way :) ) is:
BEGIN TRAN
DECLARE IdCursor CURSOR FOR SELECT Id FROM MyTable
OPEN IdCursor
FETCH NEXT FROM IdCursor INTO @CurrentId
WHILE (@@FETCH_STATUS) = 0
BEGIN
IF @@error <> 0
BEGIN
CLOSE IdCursor
DEALLOCATE IdCursor
GOTO err
END
FETCH NEXT FROM IdCursor INTO @CurrentId
END
CLOSE IdCursor
DEALLOCATE IdCursor
COMMIT TRAN
RETURN 0
err:
ROLLBACK TRAN
RETURN 1