首页 > 数据库技术 > 详细

PL/SQL学习笔记

时间:2020-03-28 11:28:15      阅读:70      评论:0      收藏:0      [点我收藏+]

一、什么是PL/SQL?

  PL/SQL直接翻译就是过程语言的sql,就是将编程语言中的语句放到数据库层面来执行,是oracle数据库对sql语言的过程化扩展,使sql语言具有了过程化处理的能力。

二、PL/SQL的程序结构?

  declare

    说明部分(变量说明、光标说明、例外说明)

  begin

    语句序列(DML语句)

  exception

    例外处理语句

  end;

  /

  1、说明部分

    基本变量类型:char,varchar2,data,number,boolean,long

    

declare
  pnumber number(7,2);
  pname varchar2(20);
  pdate date;
begin
  pnumber:=12;
  pname:=张三;
  pdate:=sysdate;
    dbms_output.put_line(pnumber);
    dbms_output.put_line(pname);
    dbms_output.put_line(pdate+1);
end;
/

    引用型变量:

    eg: my_yhdm  acl_user.yhdm%type;

    

declare
  pxm acl_user.xm%type;
  psfzh acl_user.sfzmhm%type;
begin
    select xm,sfzmhm into pxm,psfzh from acl_user where yhdm = 106030;
    dbms_output.put_line(pxm||,||psfzh);
end;
/

    记录型变量:

    eg: user_rec  acl_user%rowtype;

    

declare
  user_rec acl_user%rowtype;
begin
    select * into user_rec from acl_user where yhdm = 106090;
    dbms_output.put_line(user_rec.xm||,||user_rec.sfzmhm);
end;
/

  2、if 语句

  

set serveroutput on 
accept num prompt请输入一个数字;
declare
    pnum number:=#
begin
    if pnum = 1 then dbms_output.put_line(输入的是1);
      elsif pnum=2 then dbms_output.put_line(输入的是2);
      elsif pnum=3 then dbms_output.put_line(输入的是3);
      else dbms_output.put_line(输入的是其他数字);
    end if;
end;
/

  3、循环语句

--while循环
declare
pnum number :=1;
begin
    while pnum <=10 loop
        dbms_output.put_line(pnum);
        pnum:=pnum+1;
    end loop;
end;
/

 

--loop循环(推荐使用,控制光标方便)
set serveroutput on
declare
    pnum number :=1;
begin
 loop
    exit when pnum>10;
    dbms_output.put_line(pnum);
    pnum:=pnum+1;
 end loop;
end;
/
--for循环
set serveroutput on
declare
 pnum number:=1;
begin
    for pnum in 1..10 loop
    dbms_output.put_line(pnum);
    end loop;
end;
/

 三、PL/SQL游标

  游标就是一个结果集(Result Set)

  eg:定义游标:cursor c1 is select yhdm from acl_user;

    打开游标:open c1;

    使用游标:fetch c1 into pyhdm;

    关闭游标:close c1;

  

--游标的使用
set serveroutput on 
declare
  --定义一个游标
  cursor cuser is select xm,sfzmhm from acl_user; 
  --定义游标对应的变量
  pxm acl_user.xm%type;
  psfzmhm acl_user.sfzmhm%type;
begin
    --打开游标
    open cuser;
    loop
     fetch cuser into pxm,psfzmhm;
     exit when cuser%notfound;
     dbms_output.put_line(pxm||,||psfzmhm);
    end loop;
    --关闭游标
    close cuser;
end;
/

 

--游标的使用
set serveroutput on 
declare
  --定义一个游标
  cursor cuser is select yhdm,qsip,zzip from acl_user; 
  --定义游标对应的变量
  pyhdm acl_user.yhdm%type;
  pqsip acl_user.qsip%type;
  pzzip acl_user.zzip%type;
begin
    --打开游标
    open cuser;
    loop
     fetch cuser into pyhdm,pqsip,pzzip;
     exit when cuser%notfound;
     if pqsip is null then update acl_user set zzip = 18330108506 where yhdm = pyhdm;
     end if;
    end loop;
    
    --关闭游标
    close cuser;
    --提交
    commit;
end;

 

PL/SQL学习笔记

原文:https://www.cnblogs.com/lmspl/p/12579684.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!