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 收藏
 
相关文章
|
8天前
|
SQL 数据可视化 IDE
SQL做数据分析的困境,查询语言无法回答的真相
SQL 在简单数据分析任务中表现良好,但面对复杂需求时显得力不从心。例如,统计新用户第二天的留存率或连续活跃用户的计算,SQL 需要嵌套子查询和复杂关联,代码冗长难懂。Python 虽更灵活,但仍需变通思路,复杂度较高。相比之下,SPL(Structured Process Language)语法简洁、支持有序计算和分组子集保留,具备强大的交互性和调试功能,适合处理复杂的深度数据分析任务。SPL 已开源免费,是数据分析师的更好选择。
|
6月前
|
SQL 关系型数据库 MySQL
在MySQL中,什么是结构化查询语言 (SQL)
【8月更文挑战第20天】在MySQL中,什么是结构化查询语言 (SQL)
111 1
|
3月前
|
SQL 数据采集 监控
局域网监控电脑屏幕软件:PL/SQL 实现的数据库关联监控
在当今网络环境中,基于PL/SQL的局域网监控系统对于企业和机构的信息安全至关重要。该系统包括屏幕数据采集、数据处理与分析、数据库关联与存储三个核心模块,能够提供全面而准确的监控信息,帮助管理者有效监督局域网内的电脑使用情况。
60 2
|
4月前
|
SQL Oracle 关系型数据库
SQL语言的主要标准及其应用技巧
SQL(Structured Query Language)是数据库领域的标准语言,广泛应用于各种数据库管理系统(DBMS)中,如MySQL、Oracle、SQL Server等
158 9
|
4月前
|
SQL 关系型数据库 MySQL
Go语言项目高效对接SQL数据库:实践技巧与方法
在Go语言项目中,与SQL数据库进行对接是一项基础且重要的任务
139 11
|
6月前
|
SQL 关系型数据库 数据库
|
6月前
|
SQL 关系型数据库 MySQL
|
6月前
|
SQL Oracle 关系型数据库
SQL与PL/SQL:数据库编程语言的比较
【8月更文挑战第31天】
177 0
|
6月前
|
SQL 存储 大数据
SQL 语言发展史简直太震撼啦!从诞生到现代数据处理,见证一场奇妙的演变之旅,快来感受!
【8月更文挑战第31天】SQL(结构化查询语言)自20世纪70年代由IBM研究员E.F. Codd提出以来,已成为现代数据处理不可或缺的一部分。它最初简化了层次和网状模型中复杂的存储与检索问题,通过基本的SELECT、FROM和WHERE关键字实现了数据查询。80年代,SQL在商业数据库中广泛应用,引入了GROUP BY、HAVING和ORDER BY等功能,增强了数据分析能力。90年代,互联网和企业信息化推动了SQL的进一步优化与扩展,支持分布式数据库和数据仓库等技术。
141 0
|
6月前
|
SQL 存储 NoSQL
从SQL到NoSQL:理解不同数据库类型的选择与应用——深入比较数据模型、扩展性、查询语言、一致性和适用场景,为数据存储提供全面决策指南
【8月更文挑战第31天】在信息技术飞速发展的今天,数据库的选择至关重要。传统的SQL数据库因其稳定的事务性和强大的查询能力被广泛应用,而NoSQL数据库则凭借其灵活性和水平扩展性受到关注。本文对比了两种数据库类型的特点,帮助开发者根据应用场景做出合理选择。SQL数据库遵循关系模型,适合处理结构化数据和复杂查询;NoSQL数据库支持多种数据模型,适用于非结构化或半结构化数据。SQL数据库在一致性方面表现优异,但扩展性较差;NoSQL数据库则设计之初便考虑了水平扩展性。SQL使用成熟的SQL语言,NoSQL的查询语言更为灵活。
143 0

热门文章

最新文章