创建过程
create or replace procedure insert_dept
( num_dept in number, var_ename in varchar2, var_loc in varchar2 ) is
begin
insert into dept
values(num_dept,var_ename,var_loc);
commit;
end insert_dept;
/
调用
begin
insert_dept(79,'技术部','武汉');
end;
/
创建过程
create or replace procedure select_emp(
num_empno in emp.empno%type,
name out emp.ename%type,
salary out emp.sal%type) is
begin
select ename,sal into name,salary from emp where empno = num_empno;
exception
when no_data_found then
dbms_output.put_line('该部门不存在');
end select_emp;
/
调用
declare
var_name emp.ename%type,
var_salary emp.sal%type) is
begin
select_emp(&员工编号,var_ename,var_salary);
if var_ename is not null then
dbms_output.put_line(var_ename||' '||var_salary);
end if;
end;
/
创建过程
create or replace procedure select_emp2(
name in emp.ename%type,
salary out emp.sal%type) is
begin
select sal into salary from emp where ename = name;
exception
when no_data_found then
salary:=0;
end select_emp2;
/
调用
declare
var_sal number(5);
begin
select_emp2('&员工姓名',var_sal);
dbms_output.put_line(var_sal);
end;
/
创建过程
create or replace procedure swap(
num1 in out number,
num2 in out number)
is z number(5);
begin
z:=num1;
num1:=num2;
num2:=z;
end swap;
/
调用
declare
x number:=10;
y number:=20;
begin
dbms_output.put_line('交换前x和y的值是:'||x||' '||y);
swap(x,y);
dbms_output.put_line('交换后x和y的值是:'||x||' '||y);
END;
/
创建过程
create or replace procedure delete_emp(id dept.empno%type) is
begin
Delete from dept where empno = id;
end delete_emp;
/
调用
execute delete_emp('&员工编号');
create or replace procedure 过程名(参数)is
游标的定义;
begin
end;
/
创建过程
create or replace procedure selcet_curemp(
id in emp.deptno%type) is
cursor c1 is select * from emp where deptno = id;
begin
for rec in c1 loop
dbms_output.put_line(rec.empno||' '||rec.empno||' '||rec.job);
end loop;
end;
/
执行存储过程
execute selcet_curemp(10);
创建过程
create or replace procedure up_sal( id in number, parsent in float) is
begin
update emp set sal = sal + sal*parsent where empno = id;
end;
/
begin
up_sal(1234,0.5);
end;
/
create or replace function select_sal(id emp.empno%type) return emp.sal%type is salary emp.sal%type;
begin
select sal into salary from emp where empno = id;
return salary;
end;
/
调用函数有以下四种方式:
方式一:使用变量接收返回值
VAR salary NUMBER;
EXEC :salary:=select_sal(7369);
PRINT salary;
方式二:在SQL语句中直接调用函数
SELECT select_sal(7369) FROM DUAL;
方式三:使用DBMS_OUTPUT调用函数
EXEC dbms_output.put_line('工资是:'|| select_sal(7369));
方式四:在匿名PL/SQL块中调用函数(推荐使用该种方式-方便好记)
begin
dbms_output.put_line('工资是:'|| select_sal(7369));
end;
/
create or replace function select_sal_name(id in emp.empno%type,v_name out emp.ename%type)
return emp.sal%type is salary emp.sal%type;
begin
select sal,ename into salary,v_name from emp where empno = id;
return salary;
end;
/
declare
var_name emp.ename%type;
var_sal emp.sal%type;
begin
var_sal:=select_sal_name(7369,var_name);
dbms_output.put_line(var_name||'的工资为'||var_sal);
end;
/
create or replace function avg_sal(id emp.deptno%type) return number is
avgsal number(7,2);
begin
select avg(sal) into avgsal from emp where deptno = id;
return avgsal;
end;
/
begin
dbms_output.put_line(avg_sal(&deptno));
end;
/
create or replace function fun2 return number is
begin
update emp set sal=sal+200 where sal<(select avg(sal) from emp);
return sql%rowcount;
end;
/
begin
dbms_output.put_line(fun2);
end;
/
create or replace function find_dept(dept_no number) return dept%rowtype
is
v_dept dept%rowtype;
begin
select * into v_dept from dept where deptno = dept_no;
return v_dept;
end;
/
declare
v_dept dept%rowtype;
begin
v_dept:=find_dept(30);
dbms_output.put_line(v_dept.dname||'---'||v_dept.loc);
end;
/