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 过程已成功完成。
 
 
 
版权说明

  如果标题未标有<转载、转>等字则属于作者原创,欢迎转载,其版权归作者和博客园共有。
  作      者:温景良
  文章出处:http://wenjl520.cnblogs.com/  或  http://www.cnblogs.com/

posted @ 2009-04-28 23:44 温景良(Jason) Views( 397) Comments( 0) Edit 收藏
 

公告

 

本文转自我的程序人生博客园博客,原文链接:http://www.cnblogs.com/wenjl520/archive/2009/04/28/1445771.html,如需转载请自行联系原作者

相关文章
|
23天前
|
SQL 存储 Oracle
Oracle的PL/SQL定义变量和常量:数据的稳定与灵动
【4月更文挑战第19天】在Oracle PL/SQL中,变量和常量扮演着数据存储的关键角色。变量是可变的“魔术盒”,用于存储程序运行时的动态数据,通过`DECLARE`定义,可在循环和条件判断中体现其灵活性。常量则是不可变的“固定牌”,一旦设定值便保持不变,用`CONSTANT`声明,提供程序稳定性和易维护性。通过 `%TYPE`、`NOT NULL`等特性,可以更高效地管理和控制变量与常量,提升代码质量。善用两者,能优化PL/SQL程序的结构和性能。
|
2天前
|
SQL 数据库
数据库SQL语言实战(六)
本次实战的重点就在于对表格本身的一些处理,包括复制表格、修改表格结构、修改表格数据
|
2天前
|
SQL Oracle 关系型数据库
数据库SQL语言实战(五)(数据库系统概念第三章练习题)
本文的SQL语言适用的是Oracle数据库与mySQL可能存在略微不同
|
2天前
|
SQL Oracle 关系型数据库
数据库SQL语言实战(四)(数据库系统概念第三章练习题)
本文的SQL语言适用的是Oracle数据库与mySQL可能存在略微不同
数据库SQL语言实战(四)(数据库系统概念第三章练习题)
|
2天前
|
SQL Oracle 关系型数据库
数据库SQL语言实战(三)
本篇文章重点在于SQL中的各种删除操作
|
13天前
|
SQL 关系型数据库 MySQL
【MySQL】:探秘主流关系型数据库管理系统及SQL语言
【MySQL】:探秘主流关系型数据库管理系统及SQL语言
26 0
|
17天前
|
SQL 安全 前端开发
Go语言Gin框架安全加固:全面解析SQL注入、XSS与CSRF的解决方案
Go语言Gin框架安全加固:全面解析SQL注入、XSS与CSRF的解决方案
|
18天前
|
SQL Java 数据库连接
Java从入门到精通:2.3.2数据库编程——了解SQL语言,编写基本查询语句
Java从入门到精通:2.3.2数据库编程——了解SQL语言,编写基本查询语句
|
23天前
|
SQL Oracle 关系型数据库
Oracle的PL/SQL游标属性:数据的“导航仪”与“仪表盘”
【4月更文挑战第19天】Oracle PL/SQL游标属性如同车辆的导航仪和仪表盘,提供丰富信息和控制。 `%FOUND`和`%NOTFOUND`指示数据读取状态,`%ROWCOUNT`记录处理行数,`%ISOPEN`显示游标状态。还有`%BULK_ROWCOUNT`和`%BULK_EXCEPTIONS`增强处理灵活性。通过实例展示了如何在数据处理中利用这些属性监控和控制流程,提高效率和准确性。掌握游标属性是提升数据处理能力的关键。
|
23天前
|
SQL Oracle 安全
Oracle的PL/SQL循环语句:数据的“旋转木马”与“无限之旅”
【4月更文挑战第19天】Oracle PL/SQL中的循环语句(LOOP、EXIT WHEN、FOR、WHILE)是处理数据的关键工具,用于批量操作、报表生成和复杂业务逻辑。LOOP提供无限循环,可通过EXIT WHEN设定退出条件;FOR循环适用于固定次数迭代,WHILE循环基于条件判断执行。有效使用循环能提高效率,但需注意避免无限循环和优化大数据处理性能。掌握循环语句,将使数据处理更加高效和便捷。