oracle存储过程的编写并存入临时表

oracle存储过程的编写

create or replace procedure device_statistic is
--如果需要,在此处声明变量
online_num number(20);---在线数量
repair_num number(20);---修复数量
alarm_num number(20);---报警数量
offline_num number(20);---离线数量
str varchar2(300);
begin
   ---清空临时表数据
   execute immediate 'TRUNCATE TABLE temp_office';
  dbms_output.put_line('hello world');
  
  ---创建临时表 存储公司数据
 -- execute immediate 'CREATE TABLE temp_office AS select so.id from sys_office so where id in(select distinct p.company from pw_device p where p.del_flag=0) and so.del_flag=0';
  --str:='drop table temp_office'; 
  --execute immediate str; 
  --str:='create global temporary table temp_office(id varchar2(30))  
--on commit delete rows'; 
 -- execute immediate str; ----使用动态SQL语句来执行 
 
 ---将筛选的部门id存到临时表中
  str:='insert into temp_office (select so.id from sys_office so where id in(select distinct p.company from pw_device p where p.del_flag=0) and so.del_flag=0)'; 
  execute immediate str; 
  --commit;
  ---查询出相关数据统计
  FOR c_row IN (select d.id,b.repair_num,c.online_num,f.alarm_num,k.offline_num FROM temp_office d
    ----修复设备  
  left join (select companyid, NVL(count(alarmid),0) as repair_num from(
       select o6.id as companyid,t.id as alarmid from pw_alarm t  
        left join temp_office o6 ON o6.id = t.office_id 
        where t.create_date is not null and t.office_id is not null order by t.office_id desc
        ) 
        group by companyid) b on b.companyid= d.id 
  ---在线设备
  left join (select companyid, NVL(count(deviceid),0) as online_num from(
       select o6.id as companyid, t.on_line_state,t.id as deviceid,t.COMPANY as companys from pw_device t  
        left join temp_office o6 ON o6.id = t.COMPANY 
        where t.on_line_state='1' and t.del_flag='0' and t.company is not null order by t.company desc
        ) 
        group by companyid) c on c.companyid= d.id 
     ----报警设备 
  left join (
       select companyid, NVL(count(alarmid),0) as alarm_num from(
       select o6.id as companyid,t.id as alarmid from pw_alarm t  
        left join temp_office o6 ON o6.id = t.office_id 
        where t.office_id is not null order by t.office_id desc
        ) 
        group by companyid
) f on f.companyid= d.id 

  ---离线设备
left join (select companyid, NVL(count(deviceid),0) as offline_num from(
       select o6.id as companyid, t.on_line_state,t.id as deviceid,t.COMPANY as companys from pw_device t  
        left join temp_office o6 ON o6.id = t.COMPANY 
        where t.on_line_state='0' and t.del_flag='0' and t.company is not null order by t.company desc
        ) 
        group by companyid) k on k.companyid= d.id) LOOP
        
         --判断是否为空
           if c_row.online_num is not null then
           online_num := c_row.online_num;
           else
           online_num := 0;
           end if;
           
           if c_row.repair_num is not null then
           repair_num := c_row.repair_num;
           else
           repair_num := 0;
           end if;
           
           if c_row.alarm_num is not null then
           alarm_num := c_row.alarm_num;
           else
           alarm_num := 0;
           end if;
           
           if c_row.offline_num is not null then
           offline_num := c_row.offline_num;
           else
           offline_num := 0;
           end if;
        dbms_output.put_line('-----'||repair_num||'----'||online_num || '---------'||offline_num );
   
   ---向表中插入数据
  INSERT INTO pw_company_device_statistic
  (id,companyid,online_num,offline_num,alarm_num,repair_num,create_time)
  VALUES(SYS_GUID(),
         c_row.id,
         online_num,
         offline_num,
         alarm_num,
         repair_num,
         SYSDATE());
    ---删除临时表
     --execute immediate 'DROP TABLE temp_office';
    END LOOP;
    COMMIT;
end device_statistic;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Oracle存储过程中,你可以使用临时存储临时数据。临时是在会话级别创建的,仅在当前会话中可见,并在会话结束后自动删除。以下是使用临时的一般步骤: 1. 创建临时:使用CREATE GLOBAL TEMPORARY TABLE语句创建临时。临时可以是全局临时(GLOBAL TEMPORARY TABLE)或本地临时(LOCAL TEMPORARY TABLE)。全局临时在所有会话之间共享结构定义,但数据仅在当前会话中可见。本地临时在会话级别创建和使用,并且只在当前会话中可见。 2. 定义临时结构:在CREATE TABLE语句中定义临时的列和其他约束。 3. 在存储过程中使用临时:在存储过程中,你可以像使用任何其他一样使用临时。你可以插入、更新、删除、查询临时中的数据,并在存储过程执行期间将其作为中间结果使用。 4. 清空或删除临时:当你需要清空或删除临时中的数据时,可以使用TRUNCATE TABLE或DELETE语句。TRUNCATE TABLE语句会快速删除中的所有数据,而保留结构。DELETE语句可以根据条件删除中的数据。 5. 存储过程结束后临时自动删除:一旦存储过程执行完毕或会话结束,临时会自动删除,无需手动清理。 使用临时可以在存储过程中方便地存储和处理临时数据,提高性能和简化逻辑。注意,临时的数据仅在当前会话中可见,不会对其他会话产生影响。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值