PL/SQL 基础 ( 下 )

 

1. PL/SQL中的 SQL语句

- END语句与COMMIT等内容,没有任何关系。

- PL/SQL does not directly support data definition language( DDL ) statements, such as CREATE TA BLE, ALTER TABLE, or DROP TABLE.

- PL/SQL does not support data control language( DCL ) statements, such as GRANT or REVOKE.

- 每条 SQL语句后边都要有个 ; 分号.

- PL/SQL 可以通过设置 exception 来管理错误,例如 NO_DATA_FOUND , TOO_MANY_ROWS

2. SQL 游标 (简单介绍 )

A cursor is a private SQL work area.

2种类型 : 隐式游标,显式游标

隐式游标 : The Oracle server uses implicit cursors to parse and execute your SQL statements. ( Whenever you issue a SQL statement, the Oracle server opens an area of memory in which the command is parsed and executed. this area is called a cursor.

When the executable part of a block issues a SQL statement, PL/SQL creates an implicit cursor, which PL/SQL manages automatically.

显式游标 : Explicit cursors are explicitly declared by the programer. ( 以下为游标的属性 )

- SQL%ROWCOUNT

- SQL%FOUND

- SQL%NOTFOUND

- SQL%ISOPEN

注意此处 : 只是单纯的使用 SQL%ROWCOUNT , 并没有定义显示游标,因为这是隐式游标,证明 ORACLE在解析SQL语句时,使用隐式游标。

3. Transaction Control Statement

COMMIT [ WORK ];

SAVEPOINT savepoint_name;

ROLLBACK [WORK] ;

ROLLBACK [WORK] TO [SAVEPOINT] savepoint_name;

其中 : 貌似 WORK 没什么用.

4. 选择语句 & 循环语句

IF condition THEN

ELSIF condition THEN     // 注意,此处 ELSIF , 没有那个E

ELSE

END IF;                              // 最后,结束要有 ; 分号

CASE selector

WHEN expression1 THEN result1         // 这后边什么符号也没有

WHEN expression2 THEN result2

……

WHEN expressionN THEN resultN

ELSE resultN+1

END ;     // 要有 END 和 ; 分号

注意 NULL : not NULL 还是 NULL , 与 NULL 的计算也全部是 NULL ,

-- basic loop ( use the basic loop when the statements inside the loop must execute at least once )

LOOP

statement 1;

EXIT [WHEN condition] ;

END LOOP;

--while loop ( use the while loop if the condition has to be evaluated at the start of each iteration )

WHILE condition LOOP

statement1;

statement2;

END LOOP;

--for loop ( use a for loop if the number of iterations is known )

FOR counter IN [REVERSE]  lower_bound..upper_bound LOOP  // counter 不用自己定义,系统会直接定义 , REVERSE 表示递减, 否则表示递增,增减幅度 1

statement1;

statement2;

END LOOP;

例如 :

FOR i IN 1..3 LOOP

END LOOP;           // 增 , i 不用定义

FOR i IN REVERSE 1..3 LOOP

END LOOPS         // 减, 注意后边的区间不用去改,还是从低到高

--嵌套循环

循环之间可以互相嵌套,多层,可以使用 label 来跳到想要走的位置,label要放到对应代码的上边,

在 END LOOP 后边写上标志 ( inner_loop, outer_loop ) 是好习惯。

5. Composite Data Types

- PL/SQL records

- PL/SQL collections ( index by table , nested table, varray ) ( 貌似这种不常用 ) 
( 就是将多种相关的数据集中在一起看,比如 employeer, 有 name, salary, birthday, 等等,虽然 name, slary, birthday 他们的存储类型不同,但是由于相关性,所以将它们作为一个整体来看 )

TYPE type_name IS RECORD

( field_declaration, field_declaration );

identifier type_name;

例如 :

TYPE emp_record_type IS RECORD

( employee_id NUMBER(6) NOT NULL := 100,

last_name VARCHAR2(25),

job_id VARCHAR2(10),

salary NUMBER(8, 2) ) ;

emp_record emp_record_type;

emp_record.job_id := ‘ST_CLERK’;

这种十分类似C中的结构体

%ROWTYPE

DECLARE

emp_record   employees%ROWTYPE;

to declare a record based on a collection of columns in a database table or view, you use the %ROWTYPE attribute.

使用 ROWTYPE 的好处,1) 可以动态变更,即当基表本身发生变更时,ROWTYPE会随之变更,2)特别是在你想要将全部内容都掉出去来时,可以使用 SELECT * FROM TABLE INTO 该类型,省去了很多打字操作。

INDEX BY Tables

由两部分组成 ( 主键和列 )

- Primary key of data type BINARY_INTEGER

- Column of scalar or record data type ( 1列,或者是标准类型列,或者是组合类型列 )

TYPE type_name IS TABLE OF

{column_type variable%TYPE | table.column%TYPE | table.%ROWTYPE} [INDEX BY BINARY_INTEGER] ;

identifier type_name;

例如 :

TYPE ename_table_type IS TABLE OF

employees.last_name%TYPE

INDEX BY BINARY_INTEGER ;

ename_table ename_table_type;

( 有点像 XML )

如果定义为 INDEX BY Table 类型,会有很多方法, EXISTS , NEXT, COUNT, TRIM, FIRST AND LAST, DELETE, PRIOR

6. 权限管理相关内容

system privileges 包含 CREATE or ANY  关键字的是 系统权限

系统权限是由 SYS, SYSTEM 给予的,

Object privilege are rights assigned to a specific object within a shema and always include the name of object.

如果想创建 subprogram ( procedure , function ) , 必须有 CREATE PROCEDURE quanxian ,

if a PL/SQL subprogram refers to any objects that are not in the same schema, you must be granted access on these explicity, not through a role.

如果想调用 subprogram , 必须要有 EXECUTE object privilege.

Stored Information

Description

Access Method

General Object information The USER_OBJECTS 数据字典
Source code Text of the procedure USER_SOURCE
Parameters IN / OUT / IN OUT datatype describe command
P-code Compiled object code Not accessible
Compile errors PL/SQL syntax errors USER_ERRORS
Run-time debug information User-specified debug variables and messages The DBMS_OUTPUT

可以使用  show errors , 查看编译错误,( 这些内容也是存储在 USER_ERRORS 中的 )

DESCRIBE query_employee ( 查看 procedure 等内容, 跟 desc table_name 差不多 )

也可以是用 DBMS_OUTPUT 来显示内容,调试使用 ( 类似到处的 printf , 查看某些变量的内容 )

- 提示内容1 :Message upon entering, leaving a procedure, or indicating that an operation has occured

- 提示内容2 :Counter for a loop

- 提示内容3 :Value for a variable before and after an assignment.

DBMS_DEBUG ( 一些工具, 例如 PL/SQL DEVELOPER, 提供单步跟踪等等 )

PL/SQL Transaction Control的更多相关文章

  1. pl/sql tutorial

    http://plsql-tutorial.com/plsql-procedures.htm What is PL/SQL? PL/SQL stands for Procedural Language ...

  2. SQL with PL/SQL

    DDL commands --> create user / table / view / sequence alter DML --> data manipulation languag ...

  3. Oracle PL/SQL Articles

    我是搬运工....http://www.oracle-base.com/articles/plsql/articles-plsql.php Oracle 8i Oracle 9i Oracle 10g ...

  4. Using PL/SQL APIs as Web Services

    Overview Oracle E-Business Suite Integrated SOA Gateway allows you to use PL/SQL application program ...

  5. postgreSQL PL/SQL编程学习笔记(二)

    Control Structures of PL/SQL Control structures are probably the most useful (and important) part of ...

  6. 每周一书《Oracle 12 c PL(SQL)程序设计终极指南》

    本周为大家送出的书是<Oracle 12 c PL(SQL)程序设计终极指南>,此书由机械工业出版社出版, 孙风栋,王澜,郭晓惠 著. 内容简介: <Oracle 12c PL/SQ ...

  7. PL/SQL异常处理方法

    PL/SQL异常处理方法   1:什么是异常处理: PL/SQL提供一个功能去处理异常,在PL/SQL块中叫做异常处理,使用异常处理我们能够测试代码和避免异常退出. PL/SQL异常信息包含三个部分: ...

  8. PL/SQL : Procedural Language / Structual Query Language and it is an exrension to SQL.

    SQL is not very flexible and it cannot be made to react differently to differing sutuations easily. ...

  9. win7 64位系统 Oracle32bit + PL/SQL访问Orale服务,Oracle 11g的安装,中文乱码问题的解决

    前几天装了个Oracle32bit客户端 + PL/SQL连接不上oracle,我安装完打开PL/SQL登录界面跟正常的界面不一样,没有那个连接为Normal.SYSDBA的选项,下面有解释,至于我为 ...

随机推荐

  1. Android C代码回调java方法

    本文将讲述下列三种C代码回调java方法 1.c代码回调java空方法 2.c代码回调java int类型参数方法 3.c代码回调javaString类型参数方法 方法都差不多,先看c代码回调java ...

  2. (视频)《快速创建网站》 3.3 国际化高大上 - WordPress多语言支持

    本文是<快速创建网站>系列的第7篇,如果你还没有看过之前的内容,建议你点击以下目录中的章节先阅读其他内容再回到本文. 访问本系列目录,请点击:http://devopshub.cn/tag ...

  3. 14、SEO工程师要阅读的书籍 - IT软件人员书籍系列文章

    SEO工程师是Web项目中比较重要的一个角色.他主要负责网站的针对搜索引擎的优化方案的编写和实施.因为现在网站数量庞大,在全世界的这么多网站当中,想要让用户访问你的网站,就需要一些技巧性的内容.很多用 ...

  4. Vector和Stack(已过时,不建议使用)

    以下内容基于jdk1.7.0_79源码: 什么是Vector和Stack Vector:线程安全的动态数组 Stack:继承Vector,基于动态数组实现的一个线程安全的栈: Vector和Stack ...

  5. ios UIScrollView 中控件自动增加间隔

    设置 self.automaticallyAdjustsScrollViewInsets=FALSE; 去除自动间隔功能

  6. spring ehcache 页面、对象缓存

    一.Ehcache基本用法 CacheManager cacheManager = CacheManager.create(); // 或者 cacheManager = CacheManager.g ...

  7. 【CSharp】C#中equals与==小记

        序:        昨天技术群中的一个小伙伴发了几个字符串以及值类型比较的面试题,没想到我们的答案不尽人意...下面是截图以及答案,看看与各位看官的答案是否相同.                ...

  8. 0012 win7x64安装CentOS7

    00 准备工作 到VirtualBox官网下载Oracle VM VirtualBox 5.1.8:https://www.virtualbox.org/wiki/Downloads 到centos官 ...

  9. MySQL 调优基础(四) Linux 磁盘IO

    1. IO处理过程 磁盘IO经常会成为系统的一个瓶颈,特别是对于运行数据库的系统而言.数据从磁盘读取到内存,在到CPU缓存和寄存器,然后进行处理,最后写回磁盘,中间要经过很多的过程,下图是一个以wri ...

  10. linux 文件管理以及其相关指令

    Linux简介 严格的来讲,Linux 不算是一个操作系统,只是一个 Linux 系统中的内核, 即计算机软件与硬件通讯之间的平台:Linux的全称是GNU/Linux,这才算是一个真正意义上的Lin ...