Atribuir valor de um cursor a uma coluna da tabela
24/04/2017
0
Fiz esse cursor e está demorando demais. Até agora supera os 15 min e são apenas 934 registros.
<code>
set nocount on
declare
@id_xfc int,
@id_exm_rea int,
@id_oit int
declare exame_cur cursor
for
select distinct er.id_exm_rea from t_cmo_Exame_Realizado er
inner join t_cmo_planilha_leitura pl on er.ID_XFC = pl.ID_XFC
inner join t_cmo_exame ex on er.id_exm = ex.ID_EXM
where er.id_exm = 3936 and pl.NO_EXM = 'TÓRAX: P.A.' and er.NO_RX in(select pl.RX_NUM from t_cmo_planilha_leitura pl)
order by er.id_exm_rea
open exame_cur
fetch next from exame_cur into @id_exm_rea
select max(id_oit) as id_oit into t_id_oit_1 from t_cmo_oit1980
--insert into t_cmo_oit1980_temp1(id_oit) select id_oit from t_id_oit_1
while @@fetch_status = 0
begin
insert into t_cmo_oit1980_temp1
select
(select id_oit + 1 from t_cmo_oit1980_temp1),
(@id_exm_rea)
end
CLOSE exame_cur
DEALLOCATE exame_cur
select * from t_cmo_oit1980_temp1
go
</code>
O que eu quero é só gerar registros na tabela oit1980_temp, gerando o id_oit(PK) que é max + 1 e inserir para cada id o id_exm_rea, que é o que vem do cursor. Agora se houvesse outra forma de inserir, sem a necessidade do cursor, ficaria feliz.
<code>
set nocount on
declare
@id_xfc int,
@id_exm_rea int,
@id_oit int
declare exame_cur cursor
for
select distinct er.id_exm_rea from t_cmo_Exame_Realizado er
inner join t_cmo_planilha_leitura pl on er.ID_XFC = pl.ID_XFC
inner join t_cmo_exame ex on er.id_exm = ex.ID_EXM
where er.id_exm = 3936 and pl.NO_EXM = 'TÓRAX: P.A.' and er.NO_RX in(select pl.RX_NUM from t_cmo_planilha_leitura pl)
order by er.id_exm_rea
open exame_cur
fetch next from exame_cur into @id_exm_rea
select max(id_oit) as id_oit into t_id_oit_1 from t_cmo_oit1980
--insert into t_cmo_oit1980_temp1(id_oit) select id_oit from t_id_oit_1
while @@fetch_status = 0
begin
insert into t_cmo_oit1980_temp1
select
(select id_oit + 1 from t_cmo_oit1980_temp1),
(@id_exm_rea)
end
CLOSE exame_cur
DEALLOCATE exame_cur
select * from t_cmo_oit1980_temp1
go
</code>
O que eu quero é só gerar registros na tabela oit1980_temp, gerando o id_oit(PK) que é max + 1 e inserir para cada id o id_exm_rea, que é o que vem do cursor. Agora se houvesse outra forma de inserir, sem a necessidade do cursor, ficaria feliz.
Pjava
Curtir tópico
+ 0
Responder
Post mais votado
24/04/2017
Boa noite Pjava,
Você não incrementou o cursor dentro do While, ou seja, ficou em loop infinito e apenas no primeiro valor. o correto seria:
while @@fetch_status = 0
begin
insert into t_cmo_oit1980_temp1
select
(select id_oit + 1 from t_cmo_oit1980_temp1),
(@id_exm_rea)
FETCH NEXT FROM EXAME_CUR INTO @ID_EXM_REA
end
CLOSE exame_cur
DEALLOCATE exame_cur
Por isso teu cursor tá rodando ainda.
Tem umas melhorias que podem ser feitas, por exemplo, você não precisa pegar o MAx(id) antes.. pode pegar diretamente dentro do cursor e inserir.
Ainda podes ativar o IDENTITY a partir do valor que você preferir e com valor incremental de 1.
Enfim, o erro nesse seu caso, é o incremento do cursor.
Eu faria dessa forma que, creio eu, que seja o que você tá querendo, ou seja, inserir diretamente na t_cmo_oit1980 e não na t_cmo_oit1980_temp1:
declare
@id_exm_rea int
declare exame_cur cursor for (
select
distinct er.id_exm_rea
from t_cmo_Exame_Realizado er
inner join t_cmo_planilha_leitura pl on er.ID_XFC = pl.ID_XFC
inner join t_cmo_exame ex on er.id_exm = ex.ID_EXM
where er.id_exm = 3936 and pl.NO_EXM = 'TÓRAX: P.A.' and er.NO_RX in(select pl.RX_NUM from t_cmo_planilha_leitura pl)
order by er.id_exm_rea
)
open exame_cur
fetch next from exame_cur into @id_exm_rea
while @@fetch_status = 0
begin
insert into t_cmo_oit1980
select
(select max(id_oit) from t_cmo_oit1980) + 1,
(@id_exm_rea)
fetch next from exame_cur into @id_exm_rea
end
CLOSE exame_cur
DEALLOCATE exame_cur
select * from t_cmo_oit1980
go
abraços,
Você não incrementou o cursor dentro do While, ou seja, ficou em loop infinito e apenas no primeiro valor. o correto seria:
while @@fetch_status = 0
begin
insert into t_cmo_oit1980_temp1
select
(select id_oit + 1 from t_cmo_oit1980_temp1),
(@id_exm_rea)
FETCH NEXT FROM EXAME_CUR INTO @ID_EXM_REA
end
CLOSE exame_cur
DEALLOCATE exame_cur
Por isso teu cursor tá rodando ainda.
Tem umas melhorias que podem ser feitas, por exemplo, você não precisa pegar o MAx(id) antes.. pode pegar diretamente dentro do cursor e inserir.
Ainda podes ativar o IDENTITY a partir do valor que você preferir e com valor incremental de 1.
Enfim, o erro nesse seu caso, é o incremento do cursor.
Eu faria dessa forma que, creio eu, que seja o que você tá querendo, ou seja, inserir diretamente na t_cmo_oit1980 e não na t_cmo_oit1980_temp1:
declare
@id_exm_rea int
declare exame_cur cursor for (
select
distinct er.id_exm_rea
from t_cmo_Exame_Realizado er
inner join t_cmo_planilha_leitura pl on er.ID_XFC = pl.ID_XFC
inner join t_cmo_exame ex on er.id_exm = ex.ID_EXM
where er.id_exm = 3936 and pl.NO_EXM = 'TÓRAX: P.A.' and er.NO_RX in(select pl.RX_NUM from t_cmo_planilha_leitura pl)
order by er.id_exm_rea
)
open exame_cur
fetch next from exame_cur into @id_exm_rea
while @@fetch_status = 0
begin
insert into t_cmo_oit1980
select
(select max(id_oit) from t_cmo_oit1980) + 1,
(@id_exm_rea)
fetch next from exame_cur into @id_exm_rea
end
CLOSE exame_cur
DEALLOCATE exame_cur
select * from t_cmo_oit1980
go
abraços,
Lincon Silva
Responder
Mais Posts
26/04/2017
Pjava
Ademir, já consegui fazer, mas vou marcar a sua resposta como a correta.
Responder
Clique aqui para fazer login e interagir na Comunidade :)