zoukankan      html  css  js  c++  java
  • PL/SQL语言基础

                            PL/SQL语言基础
    /********************************数据类型*************************************/
    %rowtype  (行对象类型使用)
    变量名   表名%rowtype
    %type 
    变量名 表名.列名%TYPE=默认值
    在使用dbms_output.put_line()打印输出内容时需先设置set serveroutput on参数。
    /*****************例子******************/
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   no emp.empno%type;
      5   e  emp%rowtype;
      6  begin
      7   a:=10;
      8   b:='Axiao';
      9   select empno into no from emp where empno=7369;
     10   select * into e from emp where empno=7788;
     11   dbms_output.put_line(a);
     12   dbms_output.put_line(b);
     13   dbms_output.put_line(no);
     14   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     15* end;
     16  /
     
    PL/SQL 过程已成功完成。
     
    SQL> set serveroutput on
    SQL> /
    10
    Axiao
    7369
    7788,SCOTT,ANALYST
     
    PL/SQL 过程已成功完成。
     
    SQL> ed
    已写入文件 afiedt.buf
     
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   no emp.empno%type;
      5   e  emp%rowtype;
      6   type tab_s is table of integer index by binary_integer;
      7   t_s tab_s;
      8  begin
      9   a:=10;
     10   b:='Axiao';
     11   select empno into no from emp where empno=7369;
     12   select * into e from emp where empno=7788;
     13   t_s(3):=10;
     14   t_s(5):=20;
     15   dbms_output.put_line(a);
     16   dbms_output.put_line(b);
     17   dbms_output.put_line(no);
     18   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     19   dbms_output.put_line(t_s(3));
     20   dbms_output.put_line(t_s(5));
     21* end;
    SQL> /
    10
    Axiao
    7369
    7788,SCOTT,ANALYST
    10
    20
     
    PL/SQL 过程已成功完成。
     
    SQL> ed
    已写入文件 afiedt.buf
     
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   no emp.empno%type;
      5   e  emp%rowtype;
      6   type tab_s is table of integer index by binary_integer;
      7   t_s tab_s;
      8   type rec_s is record
      9    (subject varchar2(10),
     10     score   integer);
     11   r_s rec_s;
     12  begin
     13   a:=10;
     14   b:='Axiao';
     15   select empno into no from emp where empno=7369;
     16   select * into e from emp where empno=7788;
     17   t_s(3):=10;
     18   t_s(5):=20;
     19   r_s.subject='语文';
     20   r_s.score=70;
     21   dbms_output.put_line(a);
     22   dbms_output.put_line(b);
     23   dbms_output.put_line(no);
     24   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     25   dbms_output.put_line(t_s(3));
     26   dbms_output.put_line(t_s(5));
     27   dbms_output.put_line(r_s.subject||','||to_char(r_s.score));
     28* end;
    SQL> ed
    已写入文件 afiedt.buf
     
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   no emp.empno%type;
      5   e  emp%rowtype;
      6   type tab_s is table of integer index by binary_integer;
      7   t_s tab_s;
      8   type rec_s is record
      9    (subject varchar2(10),
     10     score   integer);
     11   r_s rec_s;
     12  begin
     13   a:=10;
     14   b:='Axiao';
     15   select empno into no from emp where empno=7369;
     16   select * into e from emp where empno=7788;
     17   t_s(3):=10;
     18   t_s(5):=20;
     19   r_s.subject:='语文';
     20   r_s.score:=70;
     21   dbms_output.put_line(a);
     22   dbms_output.put_line(b);
     23   dbms_output.put_line(no);
     24   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     25   dbms_output.put_line(t_s(3));
     26   dbms_output.put_line(t_s(5));
     27   dbms_output.put_line(r_s.subject||','||to_char(r_s.score));
     28* end;
    SQL> /
    10
    Axiao
    7369
    7788,SCOTT,ANALYST
    10
    20
    语文,70
     
    PL/SQL 过程已成功完成。
     
    SQL> ed
    已写入文件 afiedt.buf
     
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   no emp.empno%type;
      5   e  emp%rowtype;
      6   type tab_s is table of integer index by binary_integer;
      7   t_s tab_s;
      8   type rec_s is record
      9    (subject varchar2(10),
     10     score   integer);
     11   r_s rec_s;
     12  begin
     13   a:=10;
     14   b:='Axiao';
     15   select empno into no from emp where empno=9999;
     16   select * into e from emp where empno=7788;
     17   t_s(3):=10;
     18   t_s(5):=20;
     19   r_s.subject:='语文';
     20   r_s.score:=70;
     21   dbms_output.put_line(a);
     22   dbms_output.put_line(b);
     23   dbms_output.put_line(no);
     24   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     25   dbms_output.put_line(t_s(3));
     26   dbms_output.put_line(t_s(5));
     27   dbms_output.put_line(r_s.subject||','||to_char(r_s.score));
     28   exception
     29    when no_data_found then
     30       dmbs_output.put_line('没有找到合适的记录');
     31    when too_many_rows then
     32       dmbs_output.put_line('找到太多的确记录');
     33* end;
    SQL> /
         dmbs_output.put_line('没有找到合适的记录');
         *
    ERROR 位于第 30 行:
    ORA-06550: 第 30 行, 第 6 列:
    PLS-00201: 必须说明标识符 'DMBS_OUTPUT.PUT_LINE'
    ORA-06550: 第 30 行, 第 6 列:
    PL/SQL: Statement ignored
    ORA-06550: 第 32 行, 第 6 列:
    PLS-00201: 必须说明标识符 'DMBS_OUTPUT.PUT_LINE'
    ORA-06550: 第 32 行, 第 6 列:
    PL/SQL: Statement ignored
     

    SQL> ed
    已写入文件 afiedt.buf
     
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   no emp.empno%type;
      5   e  emp%rowtype;
      6   type tab_s is table of integer index by binary_integer;
      7   t_s tab_s;
      8   type rec_s is record
      9    (subject varchar2(10),
     10     score   integer);
     11   r_s rec_s;
     12  begin
     13   a:=10;
     14   b:='Axiao';
     15   select empno into no from emp where empno=9999;
     16   select * into e from emp where empno=7788;
     17   t_s(3):=10;
     18   t_s(5):=20;
     19   r_s.subject:='语文';
     20   r_s.score:=70;
     21   dbms_output.put_line(a);
     22   dbms_output.put_line(b);
     23   dbms_output.put_line(no);
     24   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     25   dbms_output.put_line(t_s(3));
     26   dbms_output.put_line(t_s(5));
     27   dbms_output.put_line(r_s.subject||','||to_char(r_s.score));
     28   exception
     29    when no_data_found then
     30       dbms_output.put_line('没有找到合适的记录');
     31    when too_many_rows then
     32       dbms_output.put_line('找到太多的确记录');
     33* end;
    SQL> /
    没有找到合适的记录
     
    PL/SQL 过程已成功完成。
     
    SQL> ed
    已写入文件 afiedt.buf
     
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   no emp.empno%type;
      5   e  emp%rowtype;
      6   type tab_s is table of integer index by binary_integer;
      7   t_s tab_s;
      8   type rec_s is record
      9    (subject varchar2(10),
     10     score   integer);
     11   r_s rec_s;
     12  begin
     13   a:=10;
     14   b:='Axiao';
     15   select empno into no from emp where job='CLERK';
     16   select * into e from emp where empno=7788;
     17   t_s(3):=10;
     18   t_s(5):=20;
     19   r_s.subject:='语文';
     20   r_s.score:=70;
     21   dbms_output.put_line(a);
     22   dbms_output.put_line(b);
     23   dbms_output.put_line(no);
     24   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     25   dbms_output.put_line(t_s(3));
     26   dbms_output.put_line(t_s(5));
     27   dbms_output.put_line(r_s.subject||','||to_char(r_s.score));
     28   exception
     29    when no_data_found then
     30       dbms_output.put_line('没有找到合适的记录');
     31    when too_many_rows then
     32       dbms_output.put_line('找到太多的确记录');
     33* end;
    SQL> /
    找到太多的确记录
     
    PL/SQL 过程已成功完成。
     
    SQL> ED
    已写入文件 afiedt.buf
     
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   c integer;
      5   no emp.empno%type;
      6   e  emp%rowtype;
      7   type tab_s is table of integer index by binary_integer;
      8   t_s tab_s;
      9   type rec_s is record
     10    (subject varchar2(10),
     11     score   integer);
     12   r_s rec_s;
     13   ee exception;
     14   pragma exception_init(ee,-1427);
     15  begin
     16   a:=10;
     17   b:='Axiao';
     18   c:=a/0;
     19   select empno into no from emp where job='CLERK';
     20   select * into e from emp where empno=7788;
     21   t_s(3):=10;
     22   t_s(5):=20;
     23   r_s.subject:='语文';
     24   r_s.score:=70;
     25   dbms_output.put_line(a);
     26   dbms_output.put_line(b);
     27   dbms_output.put_line(no);
     28   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     29   dbms_output.put_line(t_s(3));
     30   dbms_output.put_line(t_s(5));
     31   dbms_output.put_line(r_s.subject||','||to_char(r_s.score));
     32   exception
     33    when no_data_found then
     34       dbms_output.put_line('没有找到合适的记录');
     35    when too_many_rows then
     36       dbms_output.put_line('找到太多的确记录');
     37    when ee then
     38       dbms_output.put_line('除0了!');
     39* end;
    SQL> /
    declare
    *
    ERROR 位于第 1 行:
    ORA-01476: 除数为 0
    ORA-06512: 在line 18
     

    SQL> ed
    已写入文件 afiedt.buf
     
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   c integer;
      5   no emp.empno%type;
      6   e  emp%rowtype;
      7   type tab_s is table of integer index by binary_integer;
      8   t_s tab_s;
      9   type rec_s is record
     10    (subject varchar2(10),
     11     score   integer);
     12   r_s rec_s;
     13   ee exception;
     14   pragma exception_init(ee,-1427);
     15  begin
     16   a:=10;
     17   b:='Axiao';
     18   c:=a/0;
     19   select empno into no from emp where job='CLERK';
     20   select * into e from emp where empno=7788;
     21   t_s(3):=10;
     22   t_s(5):=20;
     23   r_s.subject:='语文';
     24   r_s.score:=70;
     25   dbms_output.put_line(a);
     26   dbms_output.put_line(b);
     27   dbms_output.put_line(no);
     28   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     29   dbms_output.put_line(t_s(3));
     30   dbms_output.put_line(t_s(5));
     31   dbms_output.put_line(r_s.subject||','||to_char(r_s.score));
     32   exception
     33    when others then
     34      dbms_output.put_line(sqlcode);
     35* end;
    SQL> /
    -1476
     
    PL/SQL 过程已成功完成。
     
    SQL> ed
    已写入文件 afiedt.buf
     
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   c integer;
      5   no emp.empno%type;
      6   e  emp%rowtype;
      7   type tab_s is table of integer index by binary_integer;
      8   t_s tab_s;
      9   type rec_s is record
     10    (subject varchar2(10),
     11     score   integer);
     12   r_s rec_s;
     13   ee exception;
     14   pragma exception_init(ee,-1476);
     15  begin
     16   a:=10;
     17   b:='Axiao';
     18   c:=a/0;
     19   select empno into no from emp where job='CLERK';
     20   select * into e from emp where empno=7788;
     21   t_s(3):=10;
     22   t_s(5):=20;
     23   r_s.subject:='语文';
     24   r_s.score:=70;
     25   dbms_output.put_line(a);
     26   dbms_output.put_line(b);
     27   dbms_output.put_line(no);
     28   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     29   dbms_output.put_line(t_s(3));
     30   dbms_output.put_line(t_s(5));
     31   dbms_output.put_line(r_s.subject||','||to_char(r_s.score));
     32   exception
     33    when ee then
     34      dbms_output.put_line('除0了!');
     35    when others then
     36      dbms_output.put_line(sqlcode);
     37* end;
    SQL> /
    除0了!
     
    PL/SQL 过程已成功完成。
     
    /**************************************流程控制******************************************/
    /*注:
         1条件:
               IF 条件 THEN
                  语句
               ELSIF  条件  THEN
                  语句
               ELSE
                  语句
               END IF;
         2循环:
               LOOP
                  语句
                [EXIT WHEN 条件]
               END LOOP
         3While循环
              While 条件 LOOP
              END LOOP 
    /************例子*****************/
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   c integer;
      5   no emp.empno%type;
      6   e  emp%rowtype;
      7   type tab_s is table of integer index by binary_integer;
      8   t_s tab_s;
      9   type rec_s is record
     10    (subject varchar2(10),
     11     score   integer);
     12   r_s rec_s;
     13   ee exception;
     14   pragma exception_init(ee,-1476);
     15   e1 exception;
     16  begin
     17   a:=0;
     18   b:='Axiao';
     19   if a=0 then
     20     raise e1;
     21   end if;
     22   c:=a/0;
     23   select empno into no from emp where job='CLERK';
     24   select * into e from emp where empno=7788;
     25   t_s(3):=10;
     26   t_s(5):=20;
     27   r_s.subject:='语文';
     28   r_s.score:=70;
     29   dbms_output.put_line(a);
     30   dbms_output.put_line(b);
     31   dbms_output.put_line(no);
     32   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     33   dbms_output.put_line(t_s(3));
     34   dbms_output.put_line(t_s(5));
     35   dbms_output.put_line(r_s.subject||','||to_char(r_s.score));
     36   exception
     37    when ee then
     38      dbms_output.put_line('除0了!');
     39    when e1 then
     40      dbms_output.put_line('a不可以为0了!');
     41    when others then
     42      dbms_output.put_line(sqlcode);
     43* end;
    SQL> /
    a不可以为0了!
     
    PL/SQL 过程已成功完成。
     
    SQL> ed
    已写入文件 afiedt.buf
     
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   c integer;
      5   no emp.empno%type;
      6   e  emp%rowtype;
      7   type tab_s is table of integer index by binary_integer;
      8   t_s tab_s;
      9   type rec_s is record
     10    (subject varchar2(10),
     11     score   integer);
     12   r_s rec_s;
     13   ee exception;
     14   pragma exception_init(ee,-1476);
     15   e1 exception;
     16  begin
     17   a:=0;
     18   b:='Axiao';
     19   if a=0 then
     20     raise e1;
     21   end if;
     22   c:=a/0;
     23   select empno into no from emp where job='CLERK';
     24   select * into e from emp where empno=7788;
     25   t_s(3):=10;
     26   t_s(5):=20;
     27   r_s.subject:='语文';
     28   r_s.score:=70;
     29   dbms_output.put_line(a);
     30   dbms_output.put_line(b);
     31   dbms_output.put_line(no);
     32   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     33   dbms_output.put_line(t_s(3));
     34   dbms_output.put_line(t_s(5));
     35   dbms_output.put_line(r_s.subject||','||to_char(r_s.score));
     36   exception
     37    when others then
     38      dbms_output.put_line(to_char(sqlcode)||','||sqlerrm);
     39* end;
    SQL> /
    1,User-Defined Exception
     
    PL/SQL 过程已成功完成。
     
    SQL> ed
    已写入文件 afiedt.buf
     
      1  declare
      2   a integer;
      3   b varchar2(10);
      4   c integer;
      5   no emp.empno%type;
      6   e  emp%rowtype;
      7   type tab_s is table of integer index by binary_integer;
      8   t_s tab_s;
      9   type rec_s is record
     10    (subject varchar2(10),
     11     score   integer);
     12   r_s rec_s;
     13   ee exception;
     14   pragma exception_init(ee,-1476);
     15  begin
     16   a:=0;
     17   b:='Axiao';
     18   if a=0 then
     19     raise_application_error(-20001,'A不可以为0!');
     20   end if;
     21   c:=a/0;
     22   select empno into no from emp where job='CLERK';
     23   select * into e from emp where empno=7788;
     24   t_s(3):=10;
     25   t_s(5):=20;
     26   r_s.subject:='语文';
     27   r_s.score:=70;
     28   dbms_output.put_line(a);
     29   dbms_output.put_line(b);
     30   dbms_output.put_line(no);
     31   dbms_output.put_line(to_char(e.empno)||','||e.ename||','||e.job);
     32   dbms_output.put_line(t_s(3));
     33   dbms_output.put_line(t_s(5));
     34   dbms_output.put_line(r_s.subject||','||to_char(r_s.score));
     35   exception
     36    when others then
     37      dbms_output.put_line(to_char(sqlcode)||','||sqlerrm);
     38* end;
    SQL> /
    -20001,ORA-20001: A不可以为0!
     
    PL/SQL 过程已成功完成。
     
     
     
  • 相关阅读:
    Python读excel——xlrd
    markdown demo 学习
    Markdown 语法介绍
    安全学习概览——恶意软件分析、web渗透、漏洞利用和挖掘、内网渗透、IoT安全分析、区块链、黑灰产对抗
    诺基亚 IoT安全白皮书
    北京Uber优步司机奖励政策(4月17日)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(4月17日)
    百度、腾讯和阿里内部的级别和薪资待遇是什么样的?
    成都Uber优步司机奖励政策(4月16日)
    北京Uber优步司机奖励政策(4月16日)
  • 原文地址:https://www.cnblogs.com/wenjl520/p/1445771.html
Copyright © 2011-2022 走看看