• Oracle有关于游标的操作


    这里贴一篇写的很详细的有关游标的博客,认为写的详细是因为他附带了代码,认真读一下的话很容易理解代码内部怎么实现(传送门),等我写了大作业在回来填坑,补一些常规操作。

    -----------我是分割线------

    第一次填坑,数据库大作业还的我晚上调了很长时间的bug,感觉最后做的差不多都是靠着SQL develop这个界面,显示的报错信息很详细。但我在写每周商家汇总的时候出现了一个神奇bug(主要还是我太弱了),我明明在套用两个存储过程前,我对于两个存储过程都进行了测试,但我把他们组合在一起的时候,下面oracle就提示我什么什么丢包,但是我之前写其他的过程函数嵌套的时候也没有出现什么错误啊,我只好是认为是游标出现了问题,调试了好长时间我也没有搞定,最后只好换成调用聚集函数,总算运行正常

    贴一下关于游标的示例代码:(查询近7天内所有商家的总销售额,并汇总到QQUERY表中,(其中包括在过程中建表,删表,以及日期的比较方法))

    create or replace procedure query_sal
    is
        ok number;
        t_build varchar2(400);
        cursor jj is select smno from saleman;
        res int;
        
        ans number(10);
        val number(10);
        resname varchar2(30);
        
        now_ti date;
        pre_date date;
        now_date date;
    begin
        select count(1) into ok from user_tables where table_name = upper('qquery');
        if ok>0 then
            execute immediate 'drop table qquery';
        end if;
        t_build :='
            create table qquery(
                smno number(10),
                store_name varchar2(30) not null,
                q_money number(10),
                primary key(smno)
            )
        ';
        execute immediate t_build;
        
        select sysdate - interval '7' day into pre_date from dual;
        select SYSDATE into now_date from dual;
        LOOP
            if not jj%ISOPEN then
                open jj;
            end if;
            fetch jj into res;
            EXIT WHEN jj%NOTFOUND OR jj%NOTFOUND IS NULL;
            
            select smno,sum(csmmoney) into val,ans from consume where smno=res and (csmdate between pre_date and now_date) group by smno;
            select store_name into resname from saleman where smno=res;
            insert into qquery values(res,resname,ans);
        END LOOP;
    
    end;
  • 相关阅读:
    Linux_access the file or directory which start with "-"
    Jasper_crosstab_measure_display a value of field in crosstab total row
    Linux_Shell符号及各种解释对照表
    Linux_Shell type
    Jasper_passValue_return value from the subreport to main report
    Jasper_table_resolve multiple copies of table in detail band issue
    Jasper_crosstab_headerPosition_columngroup header position config
    Linux
    un ange frappe a ma porte
    Linux
  • 原文地址:https://www.cnblogs.com/lalalatianlalu/p/10099586.html
Copyright © 2020-2023  润新知