本博文是平时整理的笔记,比较乱,完全供自己看
1、局部事物
pragma autonomous_transaction;
procedure txn_log(p_log_text in varchar2) is
pragma autonomous_transaction;
begin
if g_enable_log = c_yes then
insert into con_import_int_err_logs
(batch_id,
batch_line_id,
input_con_number,
input_con_desc,
message,
message_date)
values
(g_batch_id,
g_batch_line_id,
g_input_con_number,
g_input_con_desc,
p_log_text,
sysdate);
null;
commit;
end if;
end txn_log;
2、可以将整个查询结果插入表
insert into avic_exp_req_dtl_query_r_tmp
(session_id,
company_id) select p_session_id, p_company_id from fnd_companies;
3、导入的时候校验导入的一批数据
for循环这批数据,有错误的话就记录错误日志(局部事物)、错误标志,
for循环结束后,如果错误标志成立,就抛个异常
4、执行动态sql
v_sql := 'begin :1 := ' || v_proc_name || '( :2, :3, :4, :5); end;';
execute immediate v_sql
using out v_return, p_event.record_id, p_handle_log_id, p_event.event_param, p_event.created_by;
5、bulk collect
只适用于select into, fetch into, DML
9i之前select into 必须返回一条数据,从9i开始,可以使用bulk collect返回多行数据的结果集
declare
type emp_table_type is table of emp&rowtype index by binary_integer;
emp_table emp_table_type;
begin
select * bulk collect into emp_table from emp where deptno=&no;
for i in 1..emp_table.count loop
dbms_output.put_line('雇员姓名:' || emp_table(i).ename);
end loop;
end;
declare
type ename_table_type is table of emp.ename&TYPE;
ename_table ename_table_type;
begin
--将删除的人员信息放入结果集中
delete from emp where deptno=&no
returning ename bulk collect into ename_table;
end;
6、做数据同步的时候,可以先loop,更新已经存在的数据,然后loop,插入不存在的数据,这样子逻辑清晰,但是要loop两次表。
7、复合变量用法
declare
--定义记录类型
type gr_element is record(
emp_no varchar2(30),
emp_name varchar2(100)
);
type gc_element is table of gr_element index by binary_integer;
--定义变量
r_element gr_element;
c_element gc_element;
begin
--变量赋值
r_element.emp_no := '01';
r_element.emp_name := '张三';
dbms_output.put_line(c_element.count);
c_element(c_element.count + 1) := r_element;
dbms_output.put_line(c_element.count);
r_element.emp_no := '02';
r_element.emp_name := '李四';
c_element(c_element.count + 1) := r_element;
dbms_output.put_line(c_element.count);
for i in 1..c_element.count loop
--输出
dbms_output.put_line(c_element(i).emp_no);
dbms_output.put_line(c_element(i).emp_name);
end loop;
end;
多维表类型变量
该程序定义了名为tabletype1的多维表类型,相当于多维数组,table1是多维表类型变量,将数据表tempuser.testtable中recordnumber为60的记录提取出来
存放在table1中并显示。
�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D
declare
type tabletype1 is table of testtable%rowtype index by binary_integer;
table1 tabletype1;
begin
select * into table1(60) from tempuser.testtable where recordnumber=60;
dbms_output.put_line(table1(60).recordnumber||table1(60).currentdate);
end;
备注:在定义好的表类型变量里,可以使用count、delete、first、last、next、exists和prior等属性进行操作,使用方法为“表变量名.属性”,返回的是数字。
set serveroutput on
declare
type tabletype1 is table of varchar2(9) index by binary_integer;
table1 tabletype1;
begin
table1(1):='成都市';
table1(2):='北京市';
table1(3):='青岛市';
dbms_output.put_line('总记录数:'||to_char(table1.count));
dbms_output.put_line('第一条记录:'||table1.first);
dbms_output.put_line('最后条记录:'||table1.last);
dbms_output.put_line('第二条的前一条记录:'||table1.prior(2));
dbms_output.put_line('第二条的后一条记录:'||table1.next(2));
end;
8、dbms_job
提交job,当next_date=sysdate时,submit后JOB将立即运行,相当于dbms_job.run(job => v_job_id);
dbms_job.submit(job => v_job_id, what => v_sql);
dbms_job.submit(job => v_job_id,
what => v_sql,
next_date => v_job_detail.next_date,
interval => v_interval);
数据库中已经存在,更新job相关参数
dbms_job.change(job => v_job_detail.job_id,
what => v_sql,
next_date => v_job_detail.next_date,
interval => v_interval);
dbms_job.remove(job => v_job_id);删除
dbms_job.broken(job => v_job_id, broken => true);暂挂
dbms_job.broken(job => v_job_id, broken => false);恢复
8、动态sql
静态sql是编写pl/sql块时直接嵌入的sql语句,动态sql是在运行pl/sql块时动态输入的sql语句
动态sql处理方法
1、execute immediate 不能用于处理多行查询语句
2、open-for fetch close 处理多行查询操作
3、批量动态sql
sql := 'drop table ' || p_table_name;
sql := 'grant ' || p_priv || ' to ' || p_user;
sql := 'update emp set sal = sal * 1.1 where dept_no = 30';
execute immediate sql;
sql := 'update emp set sal = sal * (1 + :percent/100)' || ' where empno= &eno returning sal into :salary';
execute immediate sql using &1, &2 returning into salary;
动态语句中使用bulk collect
sql := 'update emp set sal=sal*(1+:percent/100)' || ' where deptno="dno' || ' returning ename, sal into :name, salary';
execute immediate sql using &percent, &dno returning bulk collect into ename_table, sal_table;
sql := 'select ename from emp where deptno=:dno';
execute immediate sql bulk collect into ename_table using &dno;
本文出自 “小杨” 博客,谢绝转载!