Jump to content

[Resolvido] Cursor com consulta


Recommended Posts

Boa noite...

eu tenho duas tabelas:

tabela1 (Id, idRaca , idEspecie)

Animal (IdA, idRaca , idEspecie, idEspecieRaca)

eu pretendo fazer um cursor que procure na tabela1 o id correspondente ao animal.idraca e ao animal.isespecie e guardar o resultado no animal.idEspecieRaca.

a minha duvida é saber onde fazer a consulta no meio do cursor de modo a devolver o valor pretendido, para posteriormente fazer o update.

Link to comment
Share on other sites

Assunto resolvido 😛

declare @especie integer
declare @IDespecieraca integer
DECLARE cursor_RacaEspecie cursor for
select IDanimal, especie, raca
from animal
where idanimal=1
open cursor_RacaEspecie
fetch NEXT from cursor_RacaEspecie INTO @COD_A,@especie,@raca

while @@FETCH_STATUS = 0
WHERE ESPECIE=@especie and RACA=@raca

 update animal
 set especieraca=@IDespecieraca
 where IDANIMAL=@cod_A
fetch next from cursor_RacaEspecie INTO @COD_A,@especie,@raca
close cursor_RacaEspecie
deallocate cursor_RacaEspecie
Edited by RitaAndrade
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.