1、位置的,名字的,混合的参数传递:
DECLAREemp_num NUMBER(6) := 120;bonus NUMBER(6) := 50;PROCEDURE raise_salary (emp_id NUMBER, amount NUMBER) ISBEGINUPDATE employees SET salary = salary + amount WHERE employee_id = emp_id;END raise_salary;BEGINraise_salary(emp_num, bonus); -- positional procedure call for actual parameters --位置传递raise_salary(amount => bonus, emp_id => emp_num); -- named parameters--名字传递用双箭头raise_salary(emp_num, amount => bonus); -- mixed parameters --混合传递END;
2、in,out,in out 三个参数模式:
再看看下面的例子:可以这样理解:in类型的在子程序块中相当于是一个常量,直接拿过来用,不能被赋值;out类型的相当于是一个变量可以在子程序块中被赋值。
DECLARE
emp_num NUMBER(6) := 120;
bonus NUMBER(6) := 50;
emp_last_name VARCHAR2(25);
PROCEDURE raise_salary (emp_id IN NUMBER, amount IN NUMBER,
emp_name OUT VARCHAR2) IS
BEGIN
UPDATE employees SET salary = salary + amount WHERE employee_id = emp_id;
SELECT last_name INTO emp_name FROM employees WHERE employee_id = emp_id;--emp_naem是out类型的,相当于变量来使。
END raise_salary;
BEGIN
raise_salary(emp_num, bonus, emp_last_name);
DBMS_OUTPUT.PUT_LINE('Salary has been updated for: ' || emp_last_name);
END;
3、子程序中默认值的使用:
4、重载,继承,多态。。DECLARE
emp_num NUMBER(6) := 120;
bonus NUMBER(6);
merit NUMBER(4);
PROCEDURE raise_salary (emp_id IN NUMBER, amount IN NUMBER DEFAULT 100, --这里有默认值。如果后面不给赋值,就用默认的咯。
extra IN NUMBER DEFAULT 50) IS
BEGIN
UPDATE employees SET salary = salary + amount + extra
WHERE employee_id = emp_id;
END raise_salary;
BEGIN
raise_salary(120); -- same as raise_salary(120, 100, 50)
raise_salary(emp_num, extra => 25);-- same as raise_salary(120, 100, 25)
END;
Example 8–11 Resolving PL/SQL Functions With InheritanceCREATE OR REPLACE TYPE super_t AS OBJECT --定义一个最高级对象,声明不是final的。(n NUMBER) NOT final;/CREATE OR REPLACE TYPE sub_t UNDER super_t --定义低一层的对象,也不是final。(n2 NUMBER) NOT final;/CREATE OR REPLACE TYPE final_t UNDER sub_t --定义最顶层的对象(n3 NUMBER);/CREATE OR REPLACE PACKAGE p ISFUNCTION func (arg super_t) RETURN NUMBER;FUNCTION func (arg sub_t) RETURN NUMBER;END;/CREATE OR REPLACE PACKAGE BODY p ISFUNCTION func (arg super_t) RETURN NUMBER IS BEGIN RETURN 1; END;FUNCTION func (arg sub_t) RETURN NUMBER IS BEGIN RETURN 2; END;END;/DECLAREv final_t := final_t(1,2,3); --这是干啥的?BEGINDBMS_OUTPUT.PUT_LINE(p.func(v)); -- prints 2 --最后输出是2,即调用了第二个函数,因为final_t距离上层近的是sut_t,而不是super_t。END;
CREATE TYPE super_t AS OBJECT(n NUMBER, MEMBER FUNCTION func RETURN NUMBER) NOT final;/CREATE TYPE BODY super_t ASMEMBER FUNCTION func RETURN NUMBER IS BEGIN RETURN 1; END; END;/CREATE OR REPLACE TYPE sub_t UNDER super_t(n2 NUMBER,OVERRIDING MEMBER FUNCTION func RETURN NUMBER) NOT final;/CREATE TYPE BODY sub_t ASOVERRIDING MEMBER FUNCTION func RETURN NUMBER IS BEGIN RETURN 2; END; END;/CREATE OR REPLACE TYPE final_t UNDER sub_t(n3 NUMBER);/DECLAREv super_t := final_t(1,2,3); --我觉得类似java中的多态呢。。。???BEGINDBMS_OUTPUT.PUT_LINE(v.func); -- prints 2 --调用的是sub_t 。。他离final近。。END;
1、先对in , out ,in out(有个NOCOPY可以加速out 和in out 类型的模式的处理速度) 类型的参数有一个理解:
declare
in_string varchar2(100) :='this is my test string';
out_string varchar2(200);
procedure double(original in varchar2, new_string out varchar2) as
begin
new_string:=original|| '+'||'original444'; --out 类型的变量可以在函数体内被赋值,但是in类型的就不行了。
exception
when value_error then
dbms_output.put_line('output buffer not long enough.');
end;
begin
double(in_string ,out_string); --传入两个参数,一个是in_string,另一个是要用来被赋值的。具体的操作在函数体内。
dbms_output.put_line(in_string ||' - ' ||out_string); --先输出一个‘this is my test string’再输出减号 “ - ” 再输出 out_string,此处即为new_string.
end;