日期:2014-05-16  浏览次数:20619 次

oracle存储过程基础语句
可以用oracle已经存在的账号scott密码triger登陆进去用里面已存在的表来做试验。
create or replace procedure lpmtest2
as 
  para1 varchar2(10);
  cursor  youbiao is  select ename from test where sal>1300;
begin
  open youbiao;
  loop
    fetch youbiao into para1;
    exit when youbiao%notfound;
    dbms_output.put_line('++:'||para1);
  end loop;
  close youbiao;
end;--最后分号记得写,否则会报错。

?======================================================================

create or replace procedure lpmtest2
as 
  cursor  youbiao is  select ename,sal,job from test where sal>1300;
  c_row youbiao%rowtype; --定义一个游标变量c_row ,该类型为游标youbiao中的一行数据类型
begin
  open youbiao;
  loop
    fetch youbiao into c_row;
    exit when youbiao%notfound;
    dbms_output.put_line('++:'||c_row.ename||':'||c_row.sal||':'||c_row.job);
  end loop;
  close youbiao;
end;

?=========================================================================

create or replace procedure lpmtest3
as
cursor c_dept is select * from dept order by deptno;
cursor c_emp(p_dept varchar2) is select ename,sal from emp where deptno=p_dept order by ename;
r_dept c_dept%rowtype;
v_ename emp.ename%type;
v_sal   emp.sal%type;
v_totalsal emp.sal%type; --用来存每个部门所有员工的总工资
begin
  open c_dept;
  loop
    fetch c_dept into r_dept;
    exit when c_dept%notfound;
    dbms_output.put_line(r_dept.deptno||':'||r_dept.dname||'+++++++++++');
    v_totalsal:=0;
    open c_emp(r_dept.deptno);
    loop
      fetch c_emp into v_ename,v_sal;
      exit when c_emp%notfound;
      dbms_output.put_line('v_ename:'||v_ename||';'||'v_sal:'||v_sal);
      v_totalsal:=v_totalsal+v_sal;
     end loop;
     close c_emp;
     dbms_output.put_line('deptsaltotal:'||v_totalsal);
   end loop;
   close c_dept;
end;

?打印出来效果:

10:ACCOUNTING+++++++++++
v_ename:CLARK;v_sal:2450
v_ename:KING;v_sal:5000
v_ename:MILLER;v_sal:1300
deptsaltotal:8750
20:RESEARCH+++++++++++
v_ename:ADAMS;v_sal:1100
v_ename:FORD;v_sal:3000
v_ename:JONES;v_sal:2975
v_ename:SCOTT;v_sal:3000
v_ename:SMITH;v_sal:800
deptsaltotal:10875
30:SALES+++++++++++
v_ename:ALLEN;v_sal:1600
v_ename:BLAKE;v_sal:2850
v_ename:JAMES;v_sal:950
v_ename:MARTIN;v_sal:1250
v_ename:TURNER;v_sal:1500
v_ename:WARD;v_sal:1250
deptsaltotal:9400
40:OPERATIONS+++++++++++
deptsaltotal:0

?======================================================================

create or replace procedure lpmtest9(v_name varchar2)
is
v_num number:=1;
begin
  loop
     insert into lpm_user values(v_name,v_num);
     commit;
     exit when v_num=5;
     v_num:=v_num+1;
  end loop;
end;

?

=======================================================================

create or replace procedure lpmtest8(lpm_empno number)
is
lpm_job emp.job%type;
begin
  select job into lpm_job from emp where empno=lpm_empno; 
  if lpm_job='PRESIDENT' then
    begin
    update emp set sal=sal+500 where empno=lpm_empno;
    commit;
    end;
  elsif lpm_job='MANAGER' then  --是elsif不是elseif!要少个e
    begin
      update emp set sal=sal+300 where empno=lpm_empno;
      commit;
    end;
  else
    begin
    update emp set sal=sal+100 where empno=lpm_empno;
    commit;
    end;
  end if;
end;

?========================================================

create or replace procedure lpmtest15(lpmEmpId emp.empno%type)
is 
addincrement emp.sal%type;
tempsal emp.sal%type;
begin
  select sal into tempsal from emp where empno=lpmEmpId;
  if tempsal>2000 then addincrement:=50;
  elsif tempsal>1500 then addincrement:=100;
  else addincrement:=150;
  end if;
  update emp set sal=sal+addincrement where empno=lpmEmpId;
  commit;
end;

?=================================================================

例: