oracle程序包的原理和使用(二)

2015-01-22 20:53:39 · 作者: · 浏览: 7
ro is student_row student %rowtype ; begin open student_cursor ; fetch student_cursor into student_row ; while student_cursor %found loop dbms_output.put_line ('学号 = ' || student_row.id || '姓名 = ' || student_row.name); fetch student_cursor into student_row ; end loop; close student_cursor ; end student_pro ; end pack2 ; / SQL > execute pack2.student_pro ; 学号 = 1姓名 = 张三 学号 = 2姓名 = 李四 学号 = 3姓名 = 王五 学号 = 4姓名 = 麻子 学号 = 5姓名 = 赵北 PL /SQL procedure successfully completed SQL > -----------------------------在程序包中创建ref游标--------------- create or replace package pack3 is type ref_cursor is ref cursor; --声明一个ref游标类型 procedure ref_student_pro ; end pack3 ; --Package created create or replace package body pack3 is procedure ref_student_pro is student_row student %rowtype ; student_ref_cursor ref_cursor ;--声明一个ref游标类型的变量 begin open student_ref_cursor for select * from student ; fetch student_ref_cursor into student_row ; while student_ref_cursor %found loop dbms_output.put_line ('学号 = ' || student_row.id || '姓名 = ' || student_row.name); fetch student_ref_cursor into student_row ; end loop; close student_ref_cursor ; end ref_student_pro ; end pack3 ; --Package body created SQL > execute pack3.ref_student_pro ; 学号 = 1姓名 = 张三 学号 = 2姓名 = 李四 学号 = 3姓名 = 王五 学号 = 4姓名 = 麻子 学号 = 5姓名 = 赵北 PL /SQL procedure successfully completed SQL >

?


系统内置程序包:
--------------------------------DBMS_job包的使用方法:------------------------------------
create table test_job (date_sign date);

create or replace procedure pro_test
is
begin
  insert into test_job values (sysdate );
  end ;
 
SQL >
variable job1 number; SQL > SQL > begin 2 dbms_job.submit (:job1 ,'pro_test;' ,sysdate ,'sysdate + 1/1440'); --Submit()过程,工作被正常地计划好。 3 end ; 4 / PL /SQL procedure successfully completed job1 --------- 23 SQL > SQL > begin 2 dbms_job.run (:job1 );-- Run()过程用来立即执行一个指定的工作。这个过 程只接收一个参数。 3 end ; 4 / PL /SQL procedure successfully completed job1 --------- 23 SQL > select * from test_job ; DATE_SIGN ----------- 19 -1 月- 15 23 SQL > select * from test_job ; DATE_SIGN ----------- 19 -1 月- 15 23 SQL > SQL > begin 2 dbms_job.remove (:job1 );--过程来删除一个已计划运行的工作。这个过程接收一个参数。 3 end ; 4 / PL /SQL procedure successfully completed job1 --------- 23 SQL > --------------------------------UTL_FILE包的使用方法:------------------------------------ create directory dir_utl_file as '/u01/app/oracle/pl_sql_pacakge/test_utl_file' ; --创建目录 grant read, write on directory dir_utl_file to hr ; --给用户赋予权限 create or replace procedure pro_utl_file (path_file in varchar2, name_file in varchar2 ) is utl_file_contents varchar2( 2000); --定义内存变量 utl_file_type utl_file.file_type ;--定义文件类型变量 begin utl_file_type := utl_file.fopen (path_file ,name_file ,'r' ,2000 );--打开文件 loop utl_file.get_line (utl_file_type ,utl_file_contents );--读取文件内容到内存变量中 dbms_output.put_line (utl_file_contents );--,并打印 end loop; exception--异常处理部分 when no_data_found then utl_file.fclose (utl_file_type ); end ; Procedure created SQL > set serverout on SQL > execute pro_utl_file ('DIR_UTL_FILE' ,'utl_file' ); DECLARE V1 VARCHAR2 (32767 ); F1 UTL_FILE.FILE_TYPE ; BEGIN -- In this example MAX_LINESIZE is less than GET_LINE'