Oracle_PL/SQL 存储过程
发布时间:2020-12-12 18:46:23 所属栏目:百科 来源:网络整理
导读:1.Oracle 提供可以吧PL/SQL程序存储在数据库中,并且可以在任何地方来运用它。这样就叫存储过程或者函数。 2.创建函数 例:返回helloworld的函数,is相当于declare用于声明局部变量,第一个return只声明返回类型 create or replace function hello_worldretur
1.Oracle 提供可以吧PL/SQL程序存储在数据库中,并且可以在任何地方来运用它。这样就叫存储过程或者函数。
2.创建函数 例:返回helloworld的函数,is相当于declare用于声明局部变量,第一个return只声明返回类型
create or replace function hello_world return vaarchar2 is begin return 'helloworld'; end; 调用该函数
select hello_world from dual; 例2:创建带参数的函数 create or replace function hello_world(v_logo varchar2) return varchar2 is begin return 'helloworld'||v_logo; endl;调用该函数 select hello_world('testLogo') from dual; 例3:返回当前系统时间的存储函数 create or replace funtion get_sysdate return date is v_date date; begin v_date :=sysdate; return v_date; end;例4:定义一个函数,获取给定部门的工资总和,要求:部门编号为参数,工资总和为返回值 create or replace funtionc get_sumsal(dept_id number) return number is v_sumsal number(10) :=0; cursor salary_cursor is select salary from employees where deptment_id=dept_id; begin for c in salary_cursor loop v_sumsal :=v_sumsal+c.salary; end loop; return v_sumsal; end; 3.关于OUT型的参数 因为函数只能有一个返回值,PL/SQL程序可以通过OUT型的参数实现有多个返回值。 例:定义一个函数,获取给定部门的工资总和,要求:部门编号为参数,工资总和为返回值,员工总数用OUT类型 create or replace funtionc get_sumsal(dept_id numbertotal_count out number) return number is v_sumsal number(10) :=0; cursor salary_cursor is select salary from employees where deptment_id=dept_id; begin total_count :=0; for c in salary_cursor loop v_sumsal :=v_sumsal+c.salary; total_count :=total_count+1; end loop; return v_sumsal; end; 调用该函数: 使用plsql调用,创建v_num变量,引入out位置。函数会在执行中赋值。 declare v_num number(5); begin dbms_output.out_line(get_sumsal(80,v_num)); dbms_output.out_line(v_num); end; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |