Tem alguma forma de fazer isto de forma mais rápida ?
Eu criei tabelas temporárias pegando apenas os dados que eu preciso cruzar, já com
os devidos filtros na cláusula "where", porém ainda vira e mexe dá pau e estourar o log e não faz tudo:
Server Message: Number 1105, Severity 17
Server 'SERVIDOR2', Procedure 'dp_migra_tabelanova53', Line 136:
Can't allocate space for object 'temp worktable' in database 'tempdb' because 'system' segment is full/has no free extents. If you ran out of space in syslogs, dump the
transaction log. Otherwise, use ALTER DATABASE or sp_extendsegment to increase size of the segment.
(123184 rows affected)
(return status = -7)
O banco é Sybase 11, e eu não tem acesso a mesma, exceto via procedure, ou seja, dependo de DBA .....
Se tiver alguma forma de fazer de 10.000 em 10.000 registros na mesma stored procedure, favor me informar.
Grato.
-----------------------------------------
set char_convert cp850
go
drop table #tabelanova53_t
go
drop table #tabela23_t
go
drop table #tabela32_t
go
drop proc dp_migra_tabelanova53
go
create proc dp_migra_tabelanova53
as
/*********************************
Cria tabelas temporárias.
**********************************/
create table #tabelanova53_t
(
cd_pta_opds varchar(8) null,
nr_pro_compl varchar(2) null,
tp_clc_prst varchar(1) null,
tp_txa_ici_opds varchar(3) null,
tp_par varchar(1) null,
cd_nr_prst varchar(3) null,
nr_seq_pag_par varchar(3) null,
dt_vct_tp_txa_ici_opds varchar(10) null,
vl_tp_txa_ici_opds varchar(17) null,
qtd_moe_rea_par varchar(15) null,
fg_mdu_bxa varchar(1) null
)
select distinct
b52.cd_pta_opds,
b60.cd_cnt_opds,
b60.tp_clc_prst,
b60.cd_ps
into
#tabela_52_60_t
from
tabela52 b52, tabela60 b60
where
(b52.cd_pta_opds = b60.cd_pta_opds) and
(tp_cbn = 4 and dt_trf_cl is null) /*** Este filtro só traz contratos ativos. ***/
select distinct
b23.cd_cnt_opds,
b23.tp_txa_ici_opds,
b23.dt_vct_tp_txa_ici_opds,
b23.vl_tp_txa_ici_opds,
b23.cd_ps
into
#tabela23_t
from
#tabela_52_60_t b60, tabela23 b23
where
(b60.cd_cnt_opds = b23.cd_cnt_opds) and
(b60.cd_ps = b23.cd_ps)
select distinct
b32.cd_cnt_opds,
b32.cd_nr_prst,
b32.fg_mdu_bxa,
b32.cd_ps
into
#tabela32_t
from
#tabela_52_60_t b60, tabela32 b32
where
(b60.cd_cnt_opds = b32.cd_cnt_opds) and
(b60.cd_ps = b32.cd_ps)
begin tran
/*********************************
Declara variáveis.
Obs.: campo_f = campo formatado (tratado).
No cursor os campos tem o mesmo tipo das tabelas de onde os dados são extraídos.
Pergunta
vilmarbr
Problema em cursor com muitos registros?
Tem alguma forma de fazer isto de forma mais rápida ?
Eu criei tabelas temporárias pegando apenas os dados que eu preciso cruzar, já com
os devidos filtros na cláusula "where", porém ainda vira e mexe dá pau e estourar o log e não faz tudo:
Server Message: Number 1105, Severity 17
Server 'SERVIDOR2', Procedure 'dp_migra_tabelanova53', Line 136:
Can't allocate space for object 'temp worktable' in database 'tempdb' because 'system' segment is full/has no free extents. If you ran out of space in syslogs, dump the
transaction log. Otherwise, use ALTER DATABASE or sp_extendsegment to increase size of the segment.
(123184 rows affected)
(return status = -7)
O banco é Sybase 11, e eu não tem acesso a mesma, exceto via procedure, ou seja, dependo de DBA .....
Se tiver alguma forma de fazer de 10.000 em 10.000 registros na mesma stored procedure, favor me informar.
Grato.
-----------------------------------------
set char_convert cp850
go
drop table #tabelanova53_t
go
drop table #tabela23_t
go
drop table #tabela32_t
go
drop proc dp_migra_tabelanova53
go
create proc dp_migra_tabelanova53
as
/*********************************
Cria tabelas temporárias.
**********************************/
create table #tabelanova53_t
(
cd_pta_opds varchar(8) null,
nr_pro_compl varchar(2) null,
tp_clc_prst varchar(1) null,
tp_txa_ici_opds varchar(3) null,
tp_par varchar(1) null,
cd_nr_prst varchar(3) null,
nr_seq_pag_par varchar(3) null,
dt_vct_tp_txa_ici_opds varchar(10) null,
vl_tp_txa_ici_opds varchar(17) null,
qtd_moe_rea_par varchar(15) null,
fg_mdu_bxa varchar(1) null
)
select distinct
b52.cd_pta_opds,
b60.cd_cnt_opds,
b60.tp_clc_prst,
b60.cd_ps
into
#tabela_52_60_t
from
tabela52 b52, tabela60 b60
where
(b52.cd_pta_opds = b60.cd_pta_opds) and
(tp_cbn = 4 and dt_trf_cl is null) /*** Este filtro só traz contratos ativos. ***/
select distinct
b23.cd_cnt_opds,
b23.tp_txa_ici_opds,
b23.dt_vct_tp_txa_ici_opds,
b23.vl_tp_txa_ici_opds,
b23.cd_ps
into
#tabela23_t
from
#tabela_52_60_t b60, tabela23 b23
where
(b60.cd_cnt_opds = b23.cd_cnt_opds) and
(b60.cd_ps = b23.cd_ps)
select distinct
b32.cd_cnt_opds,
b32.cd_nr_prst,
b32.fg_mdu_bxa,
b32.cd_ps
into
#tabela32_t
from
#tabela_52_60_t b60, tabela32 b32
where
(b60.cd_cnt_opds = b32.cd_cnt_opds) and
(b60.cd_ps = b32.cd_ps)
begin tran
/*********************************
Declara variáveis.
Obs.: campo_f = campo formatado (tratado).
No cursor os campos tem o mesmo tipo das tabelas de onde os dados são extraídos.
**********************************/
declare
@cd_pta_opds int,
@tp_clc_prst int,
@tp_txa_ici_opds int,
@cd_nr_prst int,
@dt_vct_tp_txa_ici_opds smalldatetime,
@vl_tp_txa_ici_opds decimal(9,2),
@fg_mdu_bxa varchar(1),
@msgret varchar(100),
@codret int
/*********************************
Declara o cursor a ser utilizado
**********************************/
declare cur_tabelanova53 cursor for
select distinct
b60.cd_pta_opds,
(case
when b60.tp_clc_prst = 1 then 4
when b60.tp_clc_prst = 2 then 4
when b60.tp_clc_prst = 3 then 4
when b60.tp_clc_prst = 5 then 2
when b60.tp_clc_prst = 6 then 2
when b60.tp_clc_prst = 7 then 2
when b60.tp_clc_prst = 8 then 2
when b60.tp_clc_prst = 9 then 1
when b60.tp_clc_prst = 10 then 3
when b60.tp_clc_prst = 11 then 3
when b60.tp_clc_prst = 12 then 3
end) as tp_clc_prst_f,
b23.tp_txa_ici_opds,
b32.cd_nr_prst,
b23.dt_vct_tp_txa_ici_opds,
b23.vl_tp_txa_ici_opds,
(case
when b32.fg_mdu_bxa = 'C' then '3'
when b32.fg_mdu_bxa = 'T' then '2'
when b32.fg_mdu_bxa = 'E' then '1'
end) as fg_mdu_bxa_f
from
#tabela_52_60_t b60, #tabela23_t b23, #tabela32_t b32
where
(b60.cd_cnt_opds = b23.cd_cnt_opds) and
(b60.cd_ps = b23.cd_ps) and
(b60.cd_cnt_opds = b32.cd_cnt_opds) and
(b60.cd_ps = b32.cd_ps) and
(b23.cd_ps = b32.cd_ps)
for read only
/*********************************
Abre e lê primeiro registro do cursor
**********************************/
open cur_tabelanova53
fetch cur_tabelanova53
into
@cd_pta_opds,
@tp_clc_prst,
@tp_txa_ici_opds,
@cd_nr_prst,
@dt_vct_tp_txa_ici_opds,
@vl_tp_txa_ici_opds,
@fg_mdu_bxa
/*********************************
Verifica se ainda existe registros a serem percorridos no cursor
**********************************/
while @@sqlstatus <> 2
begin
if @@sqlstatus = 1
begin
rollback tran
select @msgret = 'Procedure executada com ERRO. Problemas no cursor cur_tabelanova53.'
select @codret = -34
return -34
end
insert into #tabelanova53_t
(
cd_pta_opds,
nr_pro_compl,
tp_clc_prst,
tp_txa_ici_opds,
tp_par,
cd_nr_prst,
nr_seq_pag_par,
dt_vct_tp_txa_ici_opds,
vl_tp_txa_ici_opds,
qtd_moe_rea_par,
fg_mdu_bxa
)
values
(
convert(varchar(8),@cd_pta_opds),
'',
convert(varchar(1),@tp_clc_prst),
convert(varchar(3),@tp_txa_ici_opds),
'',
convert(varchar(3),@cd_nr_prst),
'',
convert(varchar(10),@dt_vct_tp_txa_ici_opds,104),
convert(varchar(17),@vl_tp_txa_ici_opds),
'',
@fg_mdu_bxa
)
if @@transtate = 2 or @@transtate = 3
begin
rollback tran
select @msgret = 'Executada com ERRO. Problemas ao atualizar #tabelanova53_t.'
select @codret = -10
return -10
end
commit tran
/*********************************
Lê próximo registro do cursor
**********************************/
fetch cur_tabelanova53
into
@cd_pta_opds,
@tp_clc_prst,
@tp_txa_ici_opds,
@cd_nr_prst,
@dt_vct_tp_txa_ici_opds,
@vl_tp_txa_ici_opds,
@fg_mdu_bxa
end
/**********************
Traz os campos da tabela temporária formatados: nome_campo_f.
Campo texto: completa espaços em branco com ' '.
Campo numérico: completa espaços em branco com '0'.
**********************/
select
right('00000000' + cd_pta_opds,8) as cd_pta_opds_f,
right('00' + nr_pro_compl,2) as nr_pro_compl_f,
tp_clc_prst + replicate(' ',1 - datalength(tp_clc_prst)) as tp_clc_prst_f,
right('000' + tp_txa_ici_opds,3) as tp_txa_ici_opds_f,
tp_par + replicate(' ',1 - datalength(tp_par)) as tp_par_f,
right('000' + cd_nr_prst,3) as cd_nr_prst_f,
right('000' + nr_seq_pag_par,3) as nr_seq_pag_par_f,
dt_vct_tp_txa_ici_opds + replicate(' ',10 - datalength(dt_vct_tp_txa_ici_opds)) as dt_vct_tp_txa_ici_opds_f,
right('00000000000000000' + vl_tp_txa_ici_opds,17) as vl_tp_txa_ici_opds_f,
qtd_moe_rea_par + replicate(' ',15 - datalength(qtd_moe_rea_par)) as qtd_moe_rea_par_f,
fg_mdu_bxa + replicate(' ',1 - datalength(fg_mdu_bxa)) as fg_mdu_bxa_f
from
#tabelanova53_t
close cur_tabelanova53
deallocate cursor cur_tabelanova53
return 0
go
Link para o comentário
Compartilhar em outros sites
0 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.