ORACLE PL/SQL 过程

过程

练习 1:导入型形式参数(in类型)

CREATE OR REPLACE PROCEDURE raise_salary

(p_id IN emp.empno%TYPE)

IS

--变量声明

BEGIN

UPDATE emp SET sal = sal * 1.1

WHERE empno = p_id;

END raise_salary;

/

--存储过程的调用:

EXECUTE raise_salary (7839);


declare

v_empno emp.empno%type :=&p_empno;

begin

RAISE_SALARY(v_empno);

end;

/


SQL>SHOW ERROR --查看编译错误;


插入数据:

create sequence seq_empno start with 7935;

create or replace procedure p3

(p_ename in emp.ename%type,

p_job emp.job%type default 'CLERK',

p_mgr emp.mgr%type default 7698,

p_hiredate date default sysdate,

p_sal emp.sal%type default 1000,

p_comm emp.comm%type default null,

p_deptno emp.deptno%type default 30)

is

begin

insert into emp values (seq_empno.nextval,

p_ename,

p_job,

p_mgr,

P_HIREDATE,

p_sal,

p_comm,

p_deptno);

end;

/


自治事务:

create or replace procedure p4

(v_sal emp.sal%type,v_empno emp.empno%type)

is

PRAGMA Autonomous_transaction;

begin

update emp set sal=v_sal where empno=v_empno;

commit;

end p4;

/


练习 2:导出型形式参数(out类型)

CREATE OR REPLACE PROCEDURE query_emp

(p_id IN emp.empno%TYPE,

p_name OUT emp.ename%TYPE,

p_salary OUT emp.sal%TYPE,

p_comm OUT emp.comm%TYPE)

IS

BEGIN

SELECT ename, sal, comm

INTO p_name, p_salary, p_comm

FROM emp

WHERE empno = p_id;

END query_emp;

/


VARIABLE g_name VARCHAR2(25)

VARIABLE g_sal NUMBER

VARIABLE g_comm NUMBER

EXECUTE query_emp(7369, :g_name, :g_sal, :g_comm);

PRINT


declare

v_empno emp.empno%type:=7499;

v_sal emp.sal%type;

v_ename emp.ename%type;

v_comm emp.comm%type;

begin

query_emp(v_empno,v_ename,v_sal,v_comm);

dbms_output.put_line(v_ename||' '||v_sal||' '||v_comm);

end;

/


练习 3:导入/导出型形式参数(in/out类型)

CREATE OR REPLACE PROCEDURE format_phone

(p_phone_no in out VARCHAR2)

IS

BEGIN

p_phone_no := '(' || SUBSTR(p_phone_no,1,3) ||

')' || SUBSTR(p_phone_no,4,3) ||

'-' || SUBSTR(p_phone_no,7);

END format_phone;

/


VARIABLE g_phone_no VARCHAR2(15)


exec :g_phone_no:='8006330575';


PRINT g_phone_no

EXECUTE format_phone (:g_phone_no)

PRINT g_phone_no


练习 4:带有default值的参数

CREATE OR REPLACE PROCEDURE add_dept

(p_name IN dept.dname%TYPE DEFAULT 'unknown',

p_loc IN dept.loc%TYPE DEFAULT 'BJ')

IS

BEGIN

INSERT INTO dept(deptno,

dname, loc)

VALUES (departments_seq.NEXTVAL, p_name, p_loc);

END add_dept;

/


create sequence departments_seq start with 50;


BEGIN

add_dept;

add_dept ('TRAINING', 'SH');

END;

/


练习 5:校验数据存在否

CREATE OR REPLACE PROCEDURE valid_deptno

(v_deptno IN d.deptno%TYPE,V_RETURN OUT BOOLEAN)

IS

v_dummy VARCHAR2(1);

BEGIN

SELECT 'x'

INTO v_dummy

FROM d

WHERE deptno = v_deptno;

IF SQL%FOUND THEN

V_RETURN:=TRUE;

END IF;

EXCEPTION

WHEN NO_DATA_FOUND THEN

V_RETURN:=FALSE;

END valid_deptno;

/


DECLARE

V_FLAG BOOLEAN;

BEGIN

valid_deptno(90,V_FLAG);

if v_flag then

DBMS_OUTPUT.PUT_LINE('The department exist!');

else

DBMS_OUTPUT.PUT_LINE('The department not exist!');

end if;

END;

/


练习 6:创建增加雇员的过程

CREATE OR REPLACE PROCEDURE new_emp

(v_ename emp.ename%TYPE,

v_job emp.job%TYPE DEFAULT 'SALESMAN',

v_mgr emp.mgr%TYPE DEFAULT 7839,

v_sal emp.sal%TYPE DEFAULT 1000,

v_comm emp.comm%TYPE DEFAULT 0,

v_deptno emp.deptno%TYPE DEFAULT 30)

IS

V_FLAG BOOLEAN;

BEGIN

valid_deptno(v_deptno,V_FLAG);

IF V_FLAG THEN

INSERT INTO emp

VALUES (seq_empno.NEXTVAL, v_ename, v_job, v_mgr,

TRUNC (SYSDATE, 'DD'), v_sal, v_comm, v_deptno);

ELSE

DBMS_OUTPUT.PUT_LINE('Invalid Deptno ,try again!');

END IF;

END new_emp;

/


练习 7:分页查询

create or replace procedure TABLEPAGE_SELECT

(v_page_size number,v_current_page number)

as

cursor c(v_page_size number,v_current_page number) is SELECT *

FROM (select rownum rn,emp.*

FROM emp WHERE rownum <= v_page_size*v_current_page)

WHERE rn >= (v_page_size*v_current_page-(v_page_size-1));

begin

dbms_output.put_line('pagesize : '||v_page_size||' rows');

dbms_output.put_line('current page : '||v_current_page||' page');

for r in c(v_page_size,v_current_page) loop

dbms_output.put_line('employee name : '||r.ename||' employee''s salary : '||r.sal);

end loop;

end;

/


exec TABLEPAGE_SELECT(5,2);


练习 8:本地动态SQL语句(execute immediate)

使用动态SQL创建表,建表权限不能通过角色获得!


create or replace procedure proc_test

(

  table_name in varchar2,   --表名

  field1 in varchar2,     --字段名

  datatype1 in varchar2,   --字段类型

  field2 in varchar2,     --字段名

  datatype2 in varchar2    --字段类型

) as

  str_sql varchar2(500);

begin

  str_sql:='create table '||table_name||'('||field1||' '||datatype1||','||field2||' '||datatype2||')';

  execute immediate str_sql;  --动态执行DDL语句

  exception

    when others then

      null;

end ;

/


SQL> execute proc_test('dinya_test','id','number(8) not null','name','varchar2(100)');


PL/SQL procedure successfully completed


SQL> desc dinya_test;

Name Type     Nullable Default Comments

---- ------------- -------- ------- --------

ID  NUMBER(8)


NAME VARCHAR2(100) Y


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

create or replace procedure drop_table

(v_table_name in varchar2)

is

dyn_cur number;

dyn_err varchar2(255);

begin

dyn_cur := dbms_sql.open_cursor;

dbms_sql.parse(dyn_cur,'drop table '||

v_table_name||' purge',dbms_sql.native);

dbms_sql.close_cursor(dyn_cur);

exception

when others then dyn_err := sqlerrm;

dbms_sql.close_cursor(dyn_cur);

raise_application_error(-20600,dyn_err);

end drop_table;

/


select object_name,object_type from user_objects where object_type in ('PROCEDURE','FUNCTION');

查看过程的源代码:

select text from user_source where name='TABLEPAGE_SELECT';


为procedure加密:

wrap iname=1.sql

sql>@1.plb


你可能感兴趣的:(oracle,存储过程)