Clasule while with 2 if

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

Yes. If more than one statement is after IF or ELSE, you need a BEGIN ... END:

  if not exists (SELECT id FROM dbo.proi WHERE id = @proto)
	 begin --<---
	 select retorno = @protoletra
             set @existe = 0
     end --<---
1 Like

Thanks dude!

You're welcome!