-- 修正用プログラム
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;

DECLARE的更多相关文章

  1. mysql substring函数截取值后赋给一个declare变量

    今天写的一个mysql存储过程涉及到对一个传入参数的字符串截取,然后需要判断截取字符串进行一系列操作,最开始用select  subtring() into 这样的方法将截取值赋于declare变量直 ...

  2. 关于Django 错误 doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

    记录一下 报错 doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS\ 这个问题出现没 ...

  3. rabbitmq method之queue.declare

    queue.declare即申请队列,首先对队列名作处理,若未指定队列名则随机生成一个,然后查询数据库队列是否已经创建,若创建完成则会申请队列返回 handle_method(#'queue.decl ...

  4. LINUX:read、array、declare

    read:要读取来自键盘输入的变量 使用规则: read [-pt] variale 选项与参数: -p:后面接提示字符: -t:后面接等待的“秒数”: 如果read之后不加任何参数,直接加上变量名称 ...

  5. Ubuntu 下使用declare的问题

    Ubuntu在shell的执行上用户root和普通用户是不一样的. 使用vi /etc/passwd 我们就可以看到在用户的最后一行也就是定义shell执行位置的地方root的位置是/bin/bash ...

  6. dojo分析之declare接口

    欢迎转载opendevkit文章, 文章原始地址: http://www.opendevkit.com/?e=57 declare接口是dojo定义类系统的关键函数, 类系统就是抽象,封装,继承.do ...

  7. 关于The serializable class XXX does not declare a static final serialVersionUID field of type long的警告

    编写实体类并且继承序列化接口时候,实体类会有警告,要生成一个静态的serialVersionUID. 上网搜了一下资料,现通俗解释一下: 点击前2个选项,会生成: private static fin ...

  8. PHP declare(ticks=N); 的作用

    一般用法是 declare(ticks=N);拿declare(ticks=1)来说,这句主要作用有两种: 1.Zend引擎每执行1条低级语句就去执行一次 register_tick_function ...

  9. (转)create table #temptable 临时表 和 declare @bianliang table ()表变量

    在开发过程中,经常会遇到使用表变量和本地临时表的情况.下面是对二者的一个介绍: 1. 为什么要使用表变量 表变量是从2000开始引入的,微软认为与本地临时表相比,表变量具有如下优点:  a.与其他变量 ...

  10. 有时候为了方便sql语句的对比和查询,我们可以使用declare来定义变量 上下篇的问题

    --定义一些参数 declare @nodeId int,@ekid int ,@ecid int,@eid int,@pid int --列表 SELECT * FROM ( SELECT *,RO ...

随机推荐

  1. Codevs2490 导弹防御塔

    2490 导弹防御塔 2490 导弹防御塔 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 大师 Master         题目描述 Description Freda的城堡—— ...

  2. Vue表单验证插件的制作过程

    一.表单验证模块的构成 任何表单验证模块都是由 配置――校验――报错――取值 这几部分构成的. 配置: 配置规则 和配置报错,以及优先级 校验: 有在 change 事件校验, 在点击提交按钮的时候校 ...

  3. 二分查找(BinSearch)的Javascript实现

    二分查找 解析:二分查找,也为折半查找.对于一个从小到大排列的有序数组,首先要找到一个中间值,通过与中间值比较,大的放又,小的放在左边.再在两边中寻找中间值,持续以上操作,直到找到所在位置为止. 1. ...

  4. CSS Tools: Reset CSS

    样式初始化 /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ ...

  5. cmd 运行 python

    ①cmd 进入行命令: ②输入 “python” + “空格”,即 ”python “:将已经写好的脚本文件拖拽到当前光标位置,然后敲回车运行即可.

  6. MS17-010远程溢出漏洞 - 永恒之蓝 [CVE-2017-0143]

    MS17-010远程溢出漏洞(永恒之蓝) Ti:2019-12-25 By:Mirror王宇阳 MS17-010 CVE-2017-0143 MS17-010 CVE-2017-0144 MS17-0 ...

  7. PHP学习(mysqli函数)

    php是一种运行在服务器端的程序语言,用于生产动态网页内容.特点:开源,简单,易上手,跨平台(windows,linux),占用资源少,尤其适合中小型应用开发.(微博,微信,论坛等等)一,搭建软件环境 ...

  8. Hashkell 第一篇

    心情极差.................. 无事可做,其实是没心情去做事情,只好又翻起了haskell入门 记下几点,以备查询: 1. 函数名首字符是不可以大写的, 而且名称中可以有单引号,这也是合 ...

  9. 使用 Docker/LXC 迅速启动一个桌面系统

    使用 Docker/LXC 迅速启动一个桌面系统 Docker 是 dotCloud 最近几个月刚宣布的开源引擎,旨在提供一种应用程序的自动化部署解决方案,简单的说就是,在 Linux 系统上迅速创建 ...

  10. SPSS详细操作:生存资料的Cox回归分析

    SPSS详细操作:生存资料的Cox回归分析 一.问题与数据 某研究者拟观察某新药的抗肿瘤效果,将70名肺癌患者随机分为两组,分别采用该新药和常规药物进行治疗,观察两组肺癌患者的生存情况,共随访2年.研 ...