I try make 2 level "ifs" into while, final condition is: SELECT @retorno ... OR exec procedure again, but a i recive error on 'else', incorrect syntax near keyword.
set @existe = 1
while (@existe = 1)
begin
select @protocol=cast(@@IDENTITY as varchar)
if not exists (select id from tb_d where nr like @protocol + '_')
set @proto = @protocol + @l
if not exists (SELECT id FROM dbo.proi WHERE id = @proto)
select retorno = @protoletra
set @existe = 0
else --error here
exec sp_new_protocol
end
end
end