学习存储过程中时遇到了一些相应的例题和问题,在这里和大家分享一下,希望大家多多指正
使用存储过程,查询显示emp表中各部门中最长雇佣时间
使用游标for循环完成:
set serveroutput on;
create or replace procedure my_pro1 as
cursor cur_emp is select deptno,min(hiredate) minhiredate from emp group by deptno;
begin
for x in cur_emp loop
dbms_output.put_line('部门号'||x.deptno||' 雇佣时间'||x.minhiredate);
end loop;
end;
使用普通游标完成:
create or replace procedure my_pro1 as
cursor cur_emp is select deptno ,min(hiredate) from emp group by deptno;
var_deptno emp.deptno%type;
var_hiredate emp.hiredate%type;
begin
open cur_emp;
fetch cur_emp into var_deptno,var_hiredate;
while cur_emp%found loop
dbms_output.put_line('部门号'||var_deptno||' 雇佣时间'||var_hiredate);
fetch cur_emp into var_deptno,var_hiredate;
end loop;
close cur_emp;
end;
创建一个存储过程,以部门号为参数,查询该部门的平均工资,并输出该部门中比平均工资高的员工号,员工名
首先使用普通游标来查询
create or replace procedure my_pro1(var_deptno in emp.deptno%type) as
cursor cur_emp is select empno,ename,sal from emp where deptno=var_deptno and sal>(select avg(sal) from emp where deptno=var_deptno); /*复合查询语句*/
type emp_record is record(
var_empno emp.empno%type,
var_ename emp.ename%type,
var_sal emp.sal%type
);
var_sal emp.sal%type;
x emp_record;
begin
select avg(sal) into var_sal from emp where deptno=var_deptno;
dbms_output.put_line('该部门平均工资为'||var_sal); /*输出该部门中的平均工资*/
open cur_emp;
fetch cur_emp into x;
while cur_emp%found loop
dbms_output.put_line('员工号'||x.var_empno||'员工姓名'||x.var_ename||'员工工资'||x.var_sal);
fetch cur_emp into x;
end loop;
close cur_emp;
end;
使用游标for循环完成
create or replace procedure my_pro1(var_deptno in emp.deptno%type) as
cursor emp_cur is select empno,ename,sal from emp where deptno=var_deptno and sal>(select avg(sal) from emp where deptno=var_deptno);
begin
for x in emp_cur loop
dbms_output.put_line('员工号'||x.empno||' 员工姓名'||x.ename||' 员工工资'||x.sal);
end loop;
end;
调用存储过程
execute my_pro1(10);
使用存储过程,根据员工编号显示员工信息。如果是员工工资大于2500则显示员工名和工资,否则报错。
create or replace procedure querry_emp
(e_no in emp.empno%type,e_name out emp.ename%type,e_sal out emp.sal%type)
as
null_exception exception;
begin
select ename,sal into e_name,e_sal from emp where empno=e_no;
if e_sal>2500 then dbms_output.put_line('员工姓名'||e_name||' 工资'||e_sal);
elsif e_sal<=2500
then raise null_exception;
end if;
exception
when null_exception
then dbms_output.put_line('错误');
end;
调用存储过程
declare
a1 emp.ename%type;
a2 emp.sal%type;
begin
querry_emp(7788,:a1,:a2);
end;
in 需要提前赋值,out不需要提前赋值,只需要提供一个变量,在存储过程执行完的时候,output类型的变量值就会修改,获得返回值,只能通过这个方法获得返回值,而不能像函数一样直接返回结果。因为存储过程是没有返回值的
且通过该法可以获得多个返回值。
以上是在学习存储过程中遇到的一些例题与问题,欢迎大家指正。
使用存储过程,查询显示emp表中各部门中最长雇佣时间
使用游标for循环完成:
set serveroutput on;
create or replace procedure my_pro1 as
cursor cur_emp is select deptno,min(hiredate) minhiredate from emp group by deptno;
begin
for x in cur_emp loop
dbms_output.put_line('部门号'||x.deptno||' 雇佣时间'||x.minhiredate);
end loop;
end;
使用普通游标完成:
create or replace procedure my_pro1 as
cursor cur_emp is select deptno ,min(hiredate) from emp group by deptno;
var_deptno emp.deptno%type;
var_hiredate emp.hiredate%type;
begin
open cur_emp;
fetch cur_emp into var_deptno,var_hiredate;
while cur_emp%found loop
dbms_output.put_line('部门号'||var_deptno||' 雇佣时间'||var_hiredate);
fetch cur_emp into var_deptno,var_hiredate;
end loop;
close cur_emp;
end;
创建一个存储过程,以部门号为参数,查询该部门的平均工资,并输出该部门中比平均工资高的员工号,员工名
首先使用普通游标来查询
create or replace procedure my_pro1(var_deptno in emp.deptno%type) as
cursor cur_emp is select empno,ename,sal from emp where deptno=var_deptno and sal>(select avg(sal) from emp where deptno=var_deptno); /*复合查询语句*/
type emp_record is record(
var_empno emp.empno%type,
var_ename emp.ename%type,
var_sal emp.sal%type
);
var_sal emp.sal%type;
x emp_record;
begin
select avg(sal) into var_sal from emp where deptno=var_deptno;
dbms_output.put_line('该部门平均工资为'||var_sal); /*输出该部门中的平均工资*/
open cur_emp;
fetch cur_emp into x;
while cur_emp%found loop
dbms_output.put_line('员工号'||x.var_empno||'员工姓名'||x.var_ename||'员工工资'||x.var_sal);
fetch cur_emp into x;
end loop;
close cur_emp;
end;
使用游标for循环完成
create or replace procedure my_pro1(var_deptno in emp.deptno%type) as
cursor emp_cur is select empno,ename,sal from emp where deptno=var_deptno and sal>(select avg(sal) from emp where deptno=var_deptno);
begin
for x in emp_cur loop
dbms_output.put_line('员工号'||x.empno||' 员工姓名'||x.ename||' 员工工资'||x.sal);
end loop;
end;
调用存储过程
execute my_pro1(10);
使用存储过程,根据员工编号显示员工信息。如果是员工工资大于2500则显示员工名和工资,否则报错。
create or replace procedure querry_emp
(e_no in emp.empno%type,e_name out emp.ename%type,e_sal out emp.sal%type)
as
null_exception exception;
begin
select ename,sal into e_name,e_sal from emp where empno=e_no;
if e_sal>2500 then dbms_output.put_line('员工姓名'||e_name||' 工资'||e_sal);
elsif e_sal<=2500
then raise null_exception;
end if;
exception
when null_exception
then dbms_output.put_line('错误');
end;
调用存储过程
declare
a1 emp.ename%type;
a2 emp.sal%type;
begin
querry_emp(7788,:a1,:a2);
end;
in 需要提前赋值,out不需要提前赋值,只需要提供一个变量,在存储过程执行完的时候,output类型的变量值就会修改,获得返回值,只能通过这个方法获得返回值,而不能像函数一样直接返回结果。因为存储过程是没有返回值的
且通过该法可以获得多个返回值。
以上是在学习存储过程中遇到的一些例题与问题,欢迎大家指正。