/*Create table temporária*/
CREATE TABLE t402titu_prm (
cd_emp int not null,
cd_und int not null,
cd_cli int not null,
nr_ctr numeric(10) not null,
nr_adl int not null,
sg_mod char(8) not null,
nr_nos_nr numeric(12) not null,
sg_ecg char(10) not null,
id_crt_ope char(1) null ,
id_fma_a30 char(1) null ,
id_dd_a30 char(1) null ,
tx_prm_a30 numeric(14,7) null ,
sg_mon_a30 char(10) null ,
sg_mon_m30 char(10) null ,
id_fma_m30 char(1) null ,
id_dd_m30 char(1) null ,
tx_prm_m30 numeric(14,7) null ,
tx_spr_ope numeric(14,7) null ,
id_fma_spr char(1) null ,
id_bas_spr char(1) null ,
pc_mlt numeric(5,2) null ,
qt_dd_mlt int null ,
id_clc_mlt char(1) null )
GO
/*Insere dados na temporário*/
INSERT INTO t402titu_prm
select t.cd_emp,
t.cd_und,
t.cd_cli,
t.nr_ctr,
t.nr_adl,
t.sg_mod,
t.nr_nos_nr,
e.sg_ecg,
e.id_crt_ope,
e.id_fma_a30,
e.id_dd_a30,
e.tx_prm_a30,
e.sg_mon_a30,
e.sg_mon_m30,
e.id_fma_m30,
e.id_dd_m30,
e.tx_prm_m30,
e.tx_spr_ope,
e.id_fma_spr,
e.id_bas_spr,
e.pc_mlt,
e.qt_dd_mlt,
e.id_clc_mlt
from t402titu t, t402cofi c, t402entx e
where t.cd_und = c.cd_und
and t.cd_cli = c.cd_cli
and c.sg_ecg = e.sg_ecg
and c.sg_ecg is not null
and t.sg_lin_ope not in ('COB', 'CAU', 'VIN')
and t.id_sit_lqd in ('PA', 'PP')
and t.id_crt_prm = 'C'
and t.dt_ems <= '20170831'
and e.dt_ini_vld =
(select max(dt_ini_vld) from t402entx c1 where e.sg_ecg = c1.sg_ecg)
union
select t.cd_emp,
t.cd_und,
t.cd_cli,
t.nr_ctr,
t.nr_adl,
t.sg_mod,
t.nr_nos_nr,
e.sg_ecg,
e.id_crt_ope,
e.id_fma_a30,
e.id_dd_a30,
e.tx_prm_a30,
e.sg_mon_a30,
e.sg_mon_m30,
e.id_fma_m30,
e.id_dd_m30,
e.tx_prm_m30,
e.tx_spr_ope,
e.id_fma_spr,
e.id_bas_spr,
e.pc_mlt,
e.qt_dd_mlt,
e.id_clc_mlt
from t402titu t, t402moda m, t402entx e
where t.sg_mod = m.sg_mod
and e.sg_ecg = m.sg_ecg
and m.sg_ecg is not null
and t.sg_lin_ope not in ('COB', 'CAU', 'VIN')
and t.id_sit_lqd in ('PA', 'PP')
and t.id_crt_prm = 'C'
and t.dt_ems <= '20170831'
and e.dt_ini_vld =
(select max(dt_ini_vld) from t402entx c1 where e.sg_ecg = c1.sg_ecg)
and not exists (select 1
from t402cofi cofi
where cofi.cd_und = t.cd_und and
cofi.cd_cli = t.cd_cli and
cofi.sg_ecg is not null )
/*Ajusta registro nos títulos */
update t402titu
set id_crt_prm = 'T',
id_crt_ope =
(select b.id_crt_ope
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
id_dd_a30 =
(select b.id_dd_a30
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
tx_prm_a30 =
(select b.tx_prm_a30
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
sg_id_a30 =
(select b.sg_mon_a30
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
id_fma_a30 =
(select b.id_fma_a30
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
id_dd_m30 =
(select b.id_dd_m30
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
tx_prm_m30 =
(select b.tx_prm_m30
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
sg_id_m30 =
(select b.sg_mon_m30
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
id_fma_m30 =
(select b.id_fma_m30
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
vr_tx_spr =
(select b.tx_spr_ope
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
id_fma_spr =
(select b.id_fma_spr
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
id_bas_spr =
(select b.id_bas_spr
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
pc_mlt =
(select b.pc_mlt
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
qt_dd_mlt =
(select b.qt_dd_mlt
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
id_clc_mlt =
(select b.id_clc_mlt
from t402titu_prm b
where t402titu.cd_emp = b.cd_emp
and t402titu.cd_und = b.cd_und
and t402titu.cd_cli = b.cd_cli
and t402titu.sg_mod = b.sg_mod
and t402titu.nr_ctr = b.nr_ctr
and t402titu.nr_adl = b.nr_adl
and t402titu.nr_nos_nr = b.nr_nos_nr),
id_crt_mlt = 'T'
where t402titu.id_sit_lqd in ('PA', 'PP')
and t402titu.id_crt_prm = 'C'
and exists (select 1
from t402titu_prm c
where t402titu.cd_emp = c.cd_emp
and t402titu.cd_und = c.cd_und
and t402titu.cd_cli = c.cd_cli
and t402titu.sg_mod = c.sg_mod
and t402titu.nr_ctr = c.nr_ctr
and t402titu.nr_adl = c.nr_adl
and t402titu.nr_nos_nr = c.nr_nos_nr)
update t402tiab
set id_crt_prm = 'T', id_crt_mlt = 'T'
where exists (select 1
from t402titu_prm c
where t402tiab.cd_emp = c.cd_emp
and t402tiab.cd_und = c.cd_und
and t402tiab.cd_cli = c.cd_cli
and t402tiab.sg_mod = c.sg_mod
and t402tiab.nr_ctr = c.nr_ctr
and t402tiab.nr_adl = c.nr_adl
and t402tiab.nr_nos_nr = c.nr_nos_nr)
/************************************************************************************/
select * into t402entx_res4558 from t402entx
|