-- 修正用プログラム
DECLARE
CURSOR c_adv_fee_detail IS
SELECT adv_fee.fee_mgmt_num,
adv_fee.fee_mgmt_eda,
adv_fee.receipt_num,
adv_fee.his,
adv_fee.ksai_sha_code,
adv_fee.teisei_kubun,
adv_fee.adv_baitai_bunrui_code,
adv_fee.moko_kind,
adv_fee.adv_kind_mcode,
adv_fee.adv_sha_name,
adv_fee.dan_kansan_space,
adv_fee.gross_fee,
adv_fee.input_per_code
FROM tb_adv_fee_detail adv_fee
WHERE
--料金情報(基本)に存在する(料金管理番号、枝番で結合する)
EXISTS (SELECT 1
FROM tb_fee_info_basic fee
WHERE fee.fee_mgmt_num = adv_fee.fee_mgmt_num
AND fee.fee_mgmt_eda =
‘0‘ || substr(adv_fee.fee_mgmt_eda, 2, 2))
--受付番号、履歴番号と掲載者の条件を追加して、料金情報(基本)に存在しない
AND NOT EXISTS (SELECT 1
FROM tb_fee_info_basic fee
WHERE fee.receipt_num = adv_fee.receipt_num
AND fee.his = adv_fee.his
AND fee.ksai_sha_code = adv_fee.ksai_sha_code
AND fee.fee_mgmt_num = adv_fee.fee_mgmt_num
AND fee.fee_mgmt_eda =
‘0‘ || substr(adv_fee.fee_mgmt_eda, 2, 2))
AND adv_fee.input_per_code = ‘IKOU‘
/* AND adv_fee.receipt_num = ‘W12022000286‘
AND adv_fee.his = ‘002‘
AND adv_fee.ksai_sha_code = ‘1‘*/
;
vReceiptNum tb_adv_fee_detail.receipt_num%TYPE;
vHis tb_adv_fee_detail.his%TYPE;
CURSOR c_tafd_1 IS
SELECT *
FROM (SELECT tafd.receipt_num,
tafd.his,
tafd.ksai_sha_code,
tafd.dan_kansan_space,
row_number() over(PARTITION BY tafd.receipt_num, tafd.his, tafd.ksai_sha_code ORDER BY tafd.fee_mgmt_num DESC, tafd.fee_mgmt_eda DESC) rn
FROM tb_adv_fee_detail tafd
WHERE tafd.adv_kind_mcode = ‘1‘ --記事下
AND tafd.dan_kansan_space <> 0 --段換算スペース
AND tafd.moko_kind NOT IN (‘12‘, ‘19‘) --フリー、仮版
AND tafd.teisei_kubun IN (‘2‘, ‘3‘) --黒、訂正なし
AND tafd.input_per_code = ‘IKOU‘
AND NOT EXISTS
(SELECT 1
FROM tb_tokei_dansu_kanri dan
WHERE dan.receipt_num = tafd.receipt_num
AND dan.his = tafd.his
AND dan.ksai_sha_code = tafd.ksai_sha_code
AND dan.fee_mgmt_num = ‘************‘
AND dan.fee_mgmt_eda = ‘***‘))
WHERE rn = 1;
CURSOR c_tafd_2 IS
SELECT tafd.receipt_num,
tafd.his,
tafd.ksai_sha_code,
tafd.fee_mgmt_num,
tafd.fee_mgmt_eda,
tafd.dan_kansan_space
FROM tb_adv_fee_detail tafd
WHERE ((tafd.adv_kind_mcode = ‘1‘ AND tafd.dan_kansan_space <> 0 AND
tafd.moko_kind NOT IN (‘12‘, ‘19‘)) OR
(tafd.adv_kind_mcode = ‘2‘ AND tafd.dan_kansan_space <> 0))
AND tafd.input_per_code = ‘IKOU‘
AND NOT EXISTS
(SELECT 1
FROM tb_tokei_dansu_kanri dan
WHERE dan.receipt_num = tafd.receipt_num
AND dan.his = tafd.his
AND dan.ksai_sha_code = tafd.ksai_sha_code
AND dan.fee_mgmt_num = tafd.fee_mgmt_num
AND dan.fee_mgmt_eda = tafd.fee_mgmt_eda);
vErrKey VARCHAR2(1024);
BEGIN
DELETE FROM tb_tokei_dansu_kanri dan
WHERE EXISTS
(SELECT 1
FROM tb_adv_fee_detail adv_fee
WHERE
--料金情報(基本)に存在する(料金管理番号、枝番で結合する)
EXISTS (SELECT 1
FROM tb_fee_info_basic fee
WHERE fee.fee_mgmt_num = adv_fee.fee_mgmt_num
AND fee.fee_mgmt_eda =
‘0‘ || substr(adv_fee.fee_mgmt_eda, 2, 2))
--受付番号、履歴番号と掲載者の条件を追加して、料金情報(基本)に存在しない
AND NOT EXISTS (SELECT 1
FROM tb_fee_info_basic fee
WHERE fee.receipt_num = adv_fee.receipt_num
AND fee.his = adv_fee.his
AND fee.ksai_sha_code = adv_fee.ksai_sha_code
AND fee.fee_mgmt_num = adv_fee.fee_mgmt_num
AND fee.fee_mgmt_eda =
‘0‘ || substr(adv_fee.fee_mgmt_eda, 2, 2))
AND dan.receipt_num = adv_fee.receipt_num
AND dan.his = adv_fee.his
AND dan.ksai_sha_code = adv_fee.ksai_sha_code
AND ((dan.fee_mgmt_num = ‘************‘ AND
dan.fee_mgmt_eda = ‘***‘) OR
(dan.fee_mgmt_num = adv_fee.fee_mgmt_num AND
dan.fee_mgmt_eda = adv_fee.fee_mgmt_eda))
/* AND adv_fee.receipt_num = ‘W12022000286‘
AND adv_fee.his = ‘002‘
AND adv_fee.ksai_sha_code = ‘1‘*/
);
FOR c_row IN c_adv_fee_detail
LOOP
BEGIN
vErrKey := c_row.receipt_num || ‘-‘ || c_row.his || ‘-‘ ||
c_row.ksai_sha_code || ‘-‘ || c_row.fee_mgmt_num || ‘-‘ ||
c_row.fee_mgmt_eda;
-- 料金情報基本から受付番号と履歴番号を取得
SELECT receipt_num,
his
INTO vReceiptNum,
vHis
FROM tb_fee_info_basic
WHERE fee_mgmt_num = c_row.fee_mgmt_num
AND fee_mgmt_eda = ‘0‘ || substr(c_row.fee_mgmt_eda, 2, 2);
-- 広告料金明細を更新
UPDATE tb_adv_fee_detail
SET receipt_num = vReceiptNum,
his = vHis,
update_date_time = SYSDATE,
update_per_code = ‘M0013510‘
WHERE fee_mgmt_num = c_row.fee_mgmt_num
AND fee_mgmt_eda = c_row.fee_mgmt_eda;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(‘1:‘ || vErrKey || ‘:‘ ||
SQLERRM(SQLCODE));
END;
END LOOP;
FOR c_row1 IN c_tafd_1
LOOP
BEGIN
vErrKey := c_row1.receipt_num || ‘-‘ || c_row1.his || ‘-‘ ||
c_row1.ksai_sha_code || ‘-‘ || ‘************‘ || ‘-‘ ||
‘***‘;
INSERT /*+APPEND*/
INTO tb_tokei_dansu_kanri
(receipt_num,
his,
ksai_sha_code,
fee_mgmt_num,
fee_mgmt_eda,
tokei_dansu,
input_date_time,
input_per_code,
update_date_time,
update_per_code,
update_count,
del_flg,
del_date_time,
del_per_code)
VALUES
(c_row1.receipt_num,
c_row1.his,
c_row1.ksai_sha_code,
‘************‘,
‘***‘,
c_row1.dan_kansan_space,
SYSDATE,
‘IKOU‘,
SYSDATE,
‘M0013510‘,
0,
‘0‘,
NULL,
NULL);
UPDATE tb_moko_info_ksai_sha_betsu
SET tokei_dansu = c_row1.dan_kansan_space
WHERE receipt_num = c_row1.receipt_num
AND his = c_row1.his
AND ksai_sha_code = c_row1.ksai_sha_code;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(‘2:‘ || vErrKey || ‘:‘ ||
SQLERRM(SQLCODE));
END;
END LOOP;
FOR c_row2 IN c_tafd_2
LOOP
BEGIN
vErrKey := c_row2.receipt_num || ‘-‘ || c_row2.his || ‘-‘ ||
c_row2.ksai_sha_code || ‘-‘ || c_row2.fee_mgmt_num || ‘-‘ ||
c_row2.fee_mgmt_eda;
INSERT /*+APPEND*/
INTO tb_tokei_dansu_kanri
(receipt_num,
his,
ksai_sha_code,
fee_mgmt_num,
fee_mgmt_eda,
tokei_dansu,
input_date_time,
input_per_code,
update_date_time,
update_per_code,
update_count,
del_flg,
del_date_time,
del_per_code)
VALUES
(c_row2.receipt_num,
c_row2.his,
c_row2.ksai_sha_code,
c_row2.fee_mgmt_num,
c_row2.fee_mgmt_eda,
c_row2.dan_kansan_space,
SYSDATE,
‘IKOU‘,
SYSDATE,
‘M0013510‘,
0,
‘0‘,
NULL,
NULL);
UPDATE tb_moko_info_ksai_sha_betsu
SET tokei_dansu = c_row2.dan_kansan_space
WHERE receipt_num = c_row2.receipt_num
AND his = c_row2.his
AND ksai_sha_code = c_row2.ksai_sha_code;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(‘3:‘ || vErrKey || ‘:‘ ||
SQLERRM(SQLCODE));
END;
END LOOP;
END;
原文地址:http://www.cnblogs.com/caogang/p/3810801.html