plsql 学习入门实例

函数大全:http://lib.iteye.com/blog/140210

命名规范

  标识符    命名规则        例子
  程序变量  V_name          V_name  
  程序常量  C_Name          C_company_name 
  游标变量  Name_cursor     Emp_cursor 
  异常标识  E_name          E_too_many  
  表类型    Name_table_type Emp_record_type 
  表        Name_table      Emp  
  记录类型  Name_record     Emp_record 
  SQL*Plus  替代变量        P_name P_sal  
  绑定变量  G_name          G_year_sal

创建表:

create table person (
username varchar2(20) not null,
age number not null,
money number
)

insert into person values('gbz',24,0);

1:数据类型

declare 
 --基本数据类型  变量
 v_uaername varchar2(20) not null :='gbz';
 --常量
 c_pi constant  number(6,3):=3.1414926 ;
 --声明记录类型
 type person_record is record(
    name varchar2(20) ,
    age number
 );
 --使用记录类型
 v_person person_record;
 --表类型  与map类似 key只能为数字
 type user_table_type is table of varchar2(20) index by pls_integer;  --可以是long  pls  binary  
 v_users user_table_type;
 type person_table is table of person%rowtype index by long;
 v_persontable person_table; 
 --使用表的字段类型   类型会跟着表一起变 
 v_person_age person.age%type;
 --使用表的所有字段类型   同上  
 v_person_gbz person%rowtype;
 --游标
 cursor person_cursor(p_username varchar2) is select * from person where username=p_username ;
 begin
  dbms_output.put_line('普通变量 username:'||v_uaername);--||是字符连接语句
  dbms_output.put_line('常量 age:'||c_pi);
  v_person.name:='gbz';
  v_person.age:=24;
  dbms_output.put_line('记录变量 person_name:'||v_person.name||'  age:'||v_person.age);
  v_users(1):='gbz';
  v_users(2):='张三';
  dbms_output.put_line('表类型1 '||v_users(2));
  select * into v_persontable(1) from person where username='gbz';
  dbms_output.put_line('表类型2 '||v_persontable(1).username||' money '||v_persontable(1).money);
  v_person_age:=12;
  dbms_output.put_line('使用与表字段相同的类型:'||v_person_age);
  select * into v_person_gbz from person where username='gbz';
  dbms_output.put_line('使用与表记录相同的类型:'||v_person_gbz.username);
  --游标
  open person_cursor('gbz');
  fetch person_cursor into v_person_gbz;
  close person_cursor;
  dbms_output.put_line('游标 '||v_person_gbz.username);
  --for 遍历邮标   无须打开与关闭  
  for v_person_gbz in person_cursor('gbz')
  loop 
  dbms_output.put_line(v_person_gbz.age);
  end loop;
end;

2:控制语句

declare    
 v_b boolean := false;   --可以是空值
    
 v_i number :=0;   
begin  
 if v_b then    
  dbms_output.put_line('ok');   
 elsif not v_b then  
  dbms_output.put_line('on');   
 else  
  dbms_output.put_line('null');   
 end if;     
    
 loop   
   --EXIT WHEN( i > 5 );    
   if v_i<15 then  
      dbms_output.put_line(to_char(v_i));   
      v_i:=v_i+1;   
   else  
      exit;   
   end if;   
 end loop;   
    
 while v_i<10 loop   
    dbms_output.put_line(to_char(v_i));   
    v_i:=v_i+1;   
 end loop;   
    
 for v_i in 0..10 loop   
    dbms_output.put_line(to_char(v_i));   
 end loop;   
    
end;  

 3:异常处理

     预定义异常

            

declare    
v_person person%rowtype  ;  
begin 
 select * into v_person from person where username='张三';

exception
  --预定义异常处理  无须声明
   when NO_DATA_FOUND then
       dbms_output.put_line('没有这个记录');
       dbms_output.put_line(sqlerrm);  --完整的错误信息
end;  

非预定义的异常处理

declare    
v_person person%rowtype  ;
--声明异常名 
e_notfound exception;
--与错误码关联
pragma exception_init(e_notfound,100);   --错误码即SQLCODE 
begin 
 select * into v_person from person where username='张三';

exception
   when e_notfound then
       dbms_output.put_line('没有这个记录');
       dbms_output.put_line(sqlerrm);
   when others then
       dbms_output.put_line(SQLCODE);
end;  

自定义异常

declare    
v_person person%rowtype  ;
--声明异常名 
e_found exception;

begin 
 update person set money=10 where username='gbz';
 if sql%found then
    raise e_found;
 end if;
exception
   when e_found then
       dbms_output.put_line('记录已经更新');
       dbms_output.put_line(sqlerrm);
   when others then
       dbms_output.put_line(SQLCODE);
end; 

   

declare 
 e_myException exception; --自定义异常
begin
 dbms_output.put_line('hello');
 raise e_myException;
 dbms_output.put_line('world');  --不会执行
 exception
     when e_myException then
          dbms_output.put_line(sqlcode); --当前会话执行状态,错误编码
          dbms_output.put_line(sqlerrm); --当前错误信息
          dbms_output.put_line('my error');

     when others then
          dbms_output.put_line('error');
end;

4:goto(代码逻辑混乱少用)

declare
  i number:=0;
 begin
   if i=0 then 
     goto hello;
   end if;
   <<hello>> --goto 
   begin
     dbms_output.put_line('hello');
     goto over;
   end;
   <<world>>
   begin
     dbms_output.put_line('world');
     goto over;
   end;
   <<over>>
   dbms_output.put_line('over');
 end;

5:存储过程

create or replace procedure selectperson(
p_money in number,
p_username out varchar2,
p_age in out varchar2 
)
is
v_money number :=p_money;
begin
p_username:='没有这个人';
select username into p_username from person where money>v_money;  
exception
  when others then
     p_age:=sqlerrm;
end selectperson;

 函数

CREATE OR REPLACE FUNCTION selperson(p_money in number)  
RETURN varchar2   
AS  
v_name varchar2(20) :='查无此人';  
BEGIN   
select username into v_name from person where money>p_money;
RETURN v_name   ;
exception
  when others then
    return sqlerrm; 
END selperson; 

补充

1:使用pl/sql调试存储过程

           1)右击存储过程名----》edit

            2)直接单击行号 或  右击行号---》 set Breakpoint

            3)右击存储过程名---》test

            4)在打开的窗口中可以输入参数值(窗口下方)  然后f9开始调试

2:在pl/sql命令行中运行

           edit   编辑         

           /        运行

猜你喜欢

转载自692088846.iteye.com/blog/2017245