Jump to content
RitaAndrade

[Resolvido] Cursor com consulta

Recommended Posts

RitaAndrade

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.

Share this post


Link to post
Share on other sites
RitaAndrade

Assunto resolvido :P

DECLARE @COD_A INTEGER
declare @especie integer
DECLARE @raca 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
begin
SELECT @IDespecieraca = IDESPECIERACA
FROM ESPECIERACA
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
end
close cursor_RacaEspecie
deallocate cursor_RacaEspecie

Edited by RitaAndrade

Share this post


Link to post
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.