ITPub博客

首页 > Linux操作系统 > Linux操作系统 > PL/SQL笔记.游标的使用,存储过程,函数,触发器简单示例.(注意带参数的游标很经典)

PL/SQL笔记.游标的使用,存储过程,函数,触发器简单示例.(注意带参数的游标很经典)

原创 Linux操作系统 作者:yuhan_r 时间:2010-07-08 18:33:43 0 删除 编辑

★★★----变量声明的规则
1 变量名不使用保留字
2 第一个字符必须是字母
3 变量名最多包含30个字符
4 不要与数据库的表或者列同名
5 每一行只能声明一个变量

◆◇◆---变量类型
1  binary_integer 整数 主要用来计数
2  number 数字类型
3  char   定长字符串
4  varchar2 变长字符串
5  date   日期
6  long   长字符串 最长2GB
7  boolean 布尔类型 可以取值为 true false 和 null 值

constant 相当于JAVA中的FINAL

★☆★--- 变量声明
declare
 v_empno number(4);
 v_empno2 emp.empno%type;  //用rowtype 声明
 v_empno3 v_empno2%type;
 v_name varchar2(30) not null :='myname'
begin
 dbms_output.put_line('Test')
end;

复合变量 2种  1.Table    2.Record

★☆★--Table 变量类型 数组变量类型  table 是数组的意思
declare
 type  type_table_emp_empno is table of emp.empno%type index by binary_integer;  //记住
  v_empnos type_table_emp_empno;
begin
 v_empnos(0) :=7369;
 v_empnos(1) :=8;
 v_empnos(-1) :=81;  //oracle 中可以取負值
 dbms_output.put_line(v_empnos(-1));
end;

★☆★--RECORD 类型    
declare
 type type_record_dept is record
  (deptno  dept.deptno%type,
   dname  dept.dname%type,
   loc    dept.loc%type
  );              --类似于JAVA中的类
  v_tmp type_record_dept;
begin
  v_tmp.deptno :=50;
  v_tmp.dname  :='hehe';
  v_tmp.loc    :='beijing';
  dbms_output.put_line(v_tmp.deptno||'     '||v_tmp.dname);
end;
★☆★ %rowtype 定义RECORD对象!!!不用定义类可以直接 声明对象!!!
declare
              --类似于JAVA中的类
  v_temp dept%rowtype;
begin
  v_temp.deptno :=50;
  v_temp.dname  :='hehe';
  v_temp.loc    :='beijing';
  dbms_output.put_line(v_temp.deptno||'     '||v_temp.dname);
end;

 用RECORD必须保证有且只有返回一条!!!
 
 ★☆★--DDL语句
 用二个单引号代替单引号
 begin
   execute immediate 'create table t(num number(2),name varchar2(10),school  varchar2(10) default ''aaa'')';
 end;
 /
declare
  v_sal emp.sal%type;
begin
  select sal into v_sal from emp where empno=7839;
  if (v_sal>2500) then
   v_sal :=v_sal/2;
  elsif (v_sal<2500) then
   v_sal :=v_sal*2;
  else
   dbms_output.put_line(v_sal);
  end if;
end;
  
  注意赋值符号!!!
  
  ★☆★---循环LOOP
  
do-while    循环
  declare
   i binary_integer :=1;
  begin
    loop
     dbms_output.put_line(i);
     i :=i+1;
     exit when (i>=11);
    end loop;
  end;

while       循环
declare
 i binary_integer :=1;
begin
 while(i<=11) loop
  dbms_output.put_line(i);
  i :=i+1;
  end loop;
end;

for        循环
begin
  for i in 1..10 loop     --反循环就是   for i  in reverse 1..10 loop
   dbms_output.put_line(i);
  end loop;
end;


练习

declare
 v_ename emp.ename%type;
begin
 select ename into v_ename from emp  where deptno=10;
exception
 when too_many_rows then
  dbms_output.put_line('too many rows');
 when others then
  dbms_output.put_line('error');   --这个必须放在最后!!!
end;
/
create sequence seq_errorlog_id start with 1 increment by 1;
create table errorlog(
id number primary key,
errorcode number,
errormsg varchar2(1024),
errdate date
)

declare
  v_deptno dept.deptno%type := 10;
    v_errcode errorlog.errcode%type;
    v_errmsg   errorlog.errormsg%type;
begin
  delete from dept where deptno =v_deptno;
  commit;
 exception
  when others then
    rollback;
     v_errcode :=SQLCODE;
     v_errmsg  :=SQLERRM;
     insert into errorlog values(seq_errorlog_id.nextval,v_errcode,v_errmsg,sysdate);
     commit;
end;
/


★☆★--cursor游标

declare
  cursor c is
   select * from emp;
  v_emp c%rowtype;
begin
 open c;
  fetch c into v_emp;
 dbms_output.put_line(v_emp.ename);
end;

游标循环遍历~~!!!!

declare
  cursor c         ----声明游标变量
    is select * from emp; ----
  v_emp c%rowtype;    ----声明一个RECORD对象
begin
  open c;         ----打开游标
  loop          ----循环遍历
  fetch c into v_emp;   ----把当前指向的条拿出来 并向下移一条
   exit when(c%notfound); ----在找不到下一条的时候退出。
  dbms_output.put_line(v_emp.ename); ----后台打印出        ----清除内存  不要忘记关闭游标!!!
  end loop;
close c;        ----结束循环
end; 
           
declare
 cursor c is
  select * from emp;
  v_emp emp%rowtype;
begin
 open c;
  fetch c into v_emp;
 while(c%found) loop
  dbms_output.put_line(v_emp.ename);
  fetch c into v_emp;
 end loop;
 close c;
end;
/
★☆★☆★☆★☆★☆最常用的游标遍历 FOR★☆★☆★☆★☆
declare
  cursor c is
    select * from emp;
begin
  for v_emp in c loop
    dbms_output.put_line(v_emp.ename);
  end loop;
end;
★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
带参数的游标
declare
  cursor c(v_deptno emp.deptno%type , v_job emp.job%type) is --等于与函数一起用
    select * from emp where deptno=v_deptno and job=v_job;
begin
  for v_emp in c(30,'CLERK') loop
    dbms_output.put_line(v_emp.ename);
  end loop;
end;
★☆更新游标★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
declare
 cursor c is select * from emp2 for update;             --for update 是指用于更新
begin
 for v_emp in c loop
  if(v_emp.sal<2000) then
    update emp2 set sal= sal*2 where current of c;     --where current of c 是指当前行
  elsif(v_emp.sal=5000) then
    delete from emp2  where current of c;       --删除当前行
  end if;
  end loop;
  commit;
end;

★☆★☆★☆存储过程 procedure★☆★☆★☆★☆
create or replace procedure p(v_a in number, v_b number,v_ret out number, v_temp in out number)
as
 begin
  if(v_a>v_b) then
   v_ret :=v_a;
  else
   v_ret :=v_b;
  end if;
  v_temp:=v_temp +1 ;
end;
/

函数 以前的函数 在哪用这个就在哪用!!!!
create or replace function f (v_temp number)
return number
as
begin
 if(v_temp <1500) then
  return 0.10;
 elsif(v_temp <2000) then
  return 0.15;
 else
  return 0.2;
 end if;
end;


触发器
create or replace trigger trig
 after update  or  insert  or delete  on emp2  for each row
begin
 if updating then
  dbms_output.put_line('update');
 elsif inserting then
 dbms_output.put_line('insert');
 else
  dbms_output.put_line('delete');
end;

 

 

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23877751/viewspace-667611/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-07-08

  • 博文量
    4
  • 访问量
    4602