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/

分类: Oracle
0
0
« 上一篇: 索引.序列及同义词创建和管理
» 下一篇: oracle实用sql语句
posted @ 2009-04-28 23:44 温景良(Jason) Views( 397) Comments( 0) Edit 收藏
 
相关文章
|
5月前
|
SQL 数据管理 关系型数据库
SQL 语言入门:开启数据管理的大门
在数字化时代,数据已成为核心资产,而 SQL 作为操作关系型数据库的标准语言,是数据从业者、程序员及办公人员必备技能。本文从基础概念讲起,详解 SQL 的核心用法,包括数据查询、插入、修改、删除及表结构操作,并通过实例演示帮助读者快速上手。掌握 SQL,不仅能提升数据处理效率,更为深入理解数据管理打下坚实基础。
|
4月前
|
SQL Oracle 关系型数据库
SQL语言小结
针对数据库、表单和数据行的增删改,没有涉及到sql真正的用途也就是查询,sql提供的查询语句的关键字占 sql 语言的一半之多,查询语句还是得单拿出来讲,不然太多了。 因为没有涉及到查询,所以sql的新增和修改都是很笼统的做法,drop、alter drop、delete这些很容易,逻辑性也不强,再次说明sql的真正精髓在于查询,不然为啥叫做结构化查询语言
338 0
|
4月前
|
SQL 关系型数据库 MySQL
(SQL)SQL语言中的查询语句整理
查询语句在sql中占了挺大一部分篇幅,因为在数据库中使用查询语句的次数远多于更新与删除命令。而查询语句比起其他语句要更加的复杂,可因为sql是数据库不可或缺的一部分,所以即使不懂,也必须得弄懂,以上。
320 0
|
11月前
|
SQL 存储 缓存
YashanDB SQL语言
YashanDB SQL语言
|
12月前
|
SQL 数据可视化 IDE
SQL做数据分析的困境,查询语言无法回答的真相
SQL 在简单数据分析任务中表现良好,但面对复杂需求时显得力不从心。例如,统计新用户第二天的留存率或连续活跃用户的计算,SQL 需要嵌套子查询和复杂关联,代码冗长难懂。Python 虽更灵活,但仍需变通思路,复杂度较高。相比之下,SPL(Structured Process Language)语法简洁、支持有序计算和分组子集保留,具备强大的交互性和调试功能,适合处理复杂的深度数据分析任务。SPL 已开源免费,是数据分析师的更好选择。
|
SQL 数据采集 监控
局域网监控电脑屏幕软件:PL/SQL 实现的数据库关联监控
在当今网络环境中,基于PL/SQL的局域网监控系统对于企业和机构的信息安全至关重要。该系统包括屏幕数据采集、数据处理与分析、数据库关联与存储三个核心模块,能够提供全面而准确的监控信息,帮助管理者有效监督局域网内的电脑使用情况。
203 2
|
SQL Oracle 关系型数据库
SQL语言的主要标准及其应用技巧
SQL(Structured Query Language)是数据库领域的标准语言,广泛应用于各种数据库管理系统(DBMS)中,如MySQL、Oracle、SQL Server等
480 9
|
SQL 关系型数据库 MySQL
Go语言项目高效对接SQL数据库:实践技巧与方法
在Go语言项目中,与SQL数据库进行对接是一项基础且重要的任务
317 11
|
SQL 关系型数据库 MySQL
|
SQL Oracle 关系型数据库
SQL与PL/SQL:数据库编程语言的比较
【8月更文挑战第31天】
528 1