存储过程

常用知识点:

===0、创建存储过程

create or replace procedure xxx_pro(pdate in date) is

ERR_MSG VARCHAR2(2000) := ''; --错误信息

begin

insert into xxx_LOG....

......

insert into xxx_LOG....

exception
when others then rollback;
ERR_MSG:=SQLERRM;
insert into xxx_LOG(id,Obj_Name,ATYPE,RESULT_MSG,ADD_TIME)values(seq_xxx_log.nextval,'xxx_pro',3,ERR_MSG,sysdate);
commit;

===1、游标   cursor

cursor info_cursor is --注释--
select id,name,......   from tb_xxx

begin

for d in info_cursor loop

if trunc(r.x)=trunc(pdate,'d') then
insert into xxx(id,.....) values(xxx,.....);

end if;

commit;

end loop;

===2、merge into table using()  on  xxx  when matched then  .....  when not matched then ...

merge into xxxx_tmp a using(
select xxxx,xxx
from xx
where trunc(xx)=pdate
group by gid,sid,gname
)b on(trunc(a.xx)=pdate and a.x=b.x and a.xx=b.xx)
when matched then
update set a.x=b.x  ----更新到xxxx_tmp表
when not matched then
insert values(seq_xx.nextval,xx,........);  ---插入到xxxx_tmp表
commit;

===3、 execute immediate  

 execute immediate('truncate table xxxx_tmp') ;--清空表,不管表是否存在,忽略报错

===4、INSERT INTO SELECT  VS  SELECT INTO FROM

1.INSERT INTO SELECT语句

      语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1

      要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。

2.SELECT INTO FROM语句

      语句形式为:SELECT vale1, value2 into Table2 from Table1

      要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中

原文地址:https://www.cnblogs.com/superjt/p/3336485.html