首页 > 代码库 > oracle 存储过程参数介绍 in ,out , in out 以及 执行(二)

oracle 存储过程参数介绍 in ,out , in out 以及 执行(二)

存储过程参数
1.IN DEFAULT (默认)
2.OUT
3.IN OUT 可修改

形参和实参
-- 形参

1 CREATE PROCEDURE raise_sal(ID NUMBER ,sal NUMBER) 
2 IS
3 BEGIN ...
4 END raise_sal;

-- 实参

1 emp_id := 100;
2 raise_sal(emp_id,2000);

 

-- in参数类型演示  薪水涨幅 percent

 1 CREATE OR REPLACE PROCEDURE raise_salary
 2 (
 3    ID IN employees.employee_id%TYPE ,
 4    PERCENT IN NUMBER
 5 )
 6 IS
 7 BEGIN
 8   UPDATE employees SET salary = salary * (1 + PERCENT/100) WHERE employee_id = ID;
 9 END raise_salary;
10 

-- 结果验证

1 SELECT * FROM employees WHERE employee_id = 206;
2 -- PL/SQL中执行
3 BEGIN
4 raise_salary(206,10);
5 END;
6 EXECUTE raise_salary(206,10); -- SQL命令行执行

 

-- out参数类型演示   查询指定员工的名字和薪水

 1 CREATE OR REPLACE PROCEDURE query_emp 
 2 (
 3     ID IN employees.employee_id%TYPE,
 4     NAME OUT employees.last_name%TYPE,
 5     salary OUT employees.salary%TYPE
 6 )
 7 IS 
 8 BEGIN
 9   SELECT last_name,salary INTO NAME,salary FROM employees WHERE employee_id = ID;
10 END;

 

--结果演示

 1 -- 在命令行执行
 2 SET serveroutput ON;
 3 
 4 DECLARE
 5   emp_name employees.last_name%TYPE;
 6   emp_sal employees.salary%TYPE;
 7 BEGIN
 8   query_emp(206,emp_name,emp_sal);
 9   dbms_output.put_line(name : || emp_name );
10   dbms_output.put_line(salary : || emp_sal );
11 END;
12 
13 -- 命令行执行(二)
14 VARIABLE NAME VARCHAR2(25)
15 VARIABLE sal NUMBER
16 EXECUTE query_emp(206,:NAME,:sal);

 

-- in out 类型参数
-- 参数传输方式

1.位置传输
2.名字传输 NAME=>VALUE
3.混合方式(1,2)

 1 CREATE OR REPLACE PROCEDURE add_dept
 2 (
 3     NAME IN departments.department_name%TYPE,
 4     loc IN departments.location_id%TYPE
 5 )
 6 IS
 7 BEGIN
 8   INSERT INTO departments (department_id,department_name,location_id)
 9   VALUES
10   (departments_seq.nextval,NAME,loc);
11   COMMIT;
12 END add_dept;

 

--结果验证 

1 --执行
2 EXECUTE add_dept(training,2500);
3 --验证
4 SELECT * FROM departments WHERE location_id= 2500;
5 --执行方式二
6 EXECUTE add_dept(loc=>2400,NAME=>education);
7 --验证
8 SELECT * FROM departments WHERE location_id= 2400;

 

-- 使用默认值定义
-- NAME departments.department_name%TYPE = ‘UNKNOW‘,
-- loc departments.location_id%TYPE = 1800

 

 1 CREATE  OR  REPLACE PROCEDURE add_dept
 2 (
 3     NAME departments.department_name%TYPE := UNKNOW,
 4     loc  departments.location_id%TYPE := 1800
 5 )
 6 IS
 7 BEGIN
 8   INSERT INTO departments (department_id,department_name,location_id)
 9   VALUES
10   (departments_seq.nextval,NAME,loc);
11   COMMIT;
12 END add_dept;

 

-- 结果演示

 

1 --执行
2 EXECUTE add_dept();
3 --验证
4 SELECT * FROM departments WHERE location_id= 1800;

 

oracle 存储过程参数介绍 in ,out , in out 以及 执行(二)