Oracle的PL/SQL语句介绍
2013-03-18 20:53
591 查看
pl/sql 三个组成部分:(1)变量声明部分(2)可执行部分 (3)异常处理部分
(1)变量声明部分
a 、 首先必须给予变量正确的名称(命名规范)
注意与SQL SERVER区分:在SQL SERVER 中自定义的变量命名以@开头
b 、赋予变量正确的数据类型
能赋予的数据类型:oracle自带的数据类型(varchar2(n),number(p,s),date)
oracle支持的数据类型(integer, string )
表名.字段名%type,表名%rowtype
记录类型(record) type record_type is record(id number(10),name varchar2(20));
异常变量类型 (exception)
c、在声明部分给变量赋予初值的两种方式: 1、:= 2、default
d、常量的使用(注意声明常量的时候就必须赋予初值,常量的值不能更改的)
(2)可执行部分
a、增、删、改语句以及"select..into .. from .."可以直接放置在begin..end之间
b、在可执行部分给变量赋予初值的两种方式: 1、 := 2、select..into .. from ..
c、dbms_output.put_line() 以及 dbms_output.put() 还有dbms_output.new_line之间的关联以及区别
d、流程控制语句
if 分支
if ... then
..
elsif .. then --注意elsif
...
else
...
end if;
--并列if
if .. then
..
end if;
if .. then
..
end if;
--嵌套if
if .. then
if .. then
...
end if;
end if;
case 分支
case
when .. then ..;
when .. then ..;
else
...
end case;
--基本循环
loop
--退出循环的两种写法
if ... then
exit;--退出
end if;
--第二种写法
exit when 条件;
end loop;
--while循环
while 条件
loop
...
end loop;
--for循环
for x in [reverse] value1..value2
loop
.....
--x := x+1; 错误的
end loop;
--注意:x是不需要声明的 value1的值一般都是小于value2,如果反向采用reverse关键字
--x的值不能够显示的修改
--goto语句 以及 null语句
--null语句最大的一个好处就是维护我们正常的语法规则
declare
x number;
y varchar2(20);
begin
x:=3;
case
when x=1 then y:=1;
when x=2 then y:=2;
when x=3 then null;
else
y:=4;
end case;
dbms_output.put_line(y);
end;
e、动态SQL语句
语法:
execute immediate '动态SQL语句'
[into ...]
[using ...]
begin
create table test(tno number,tname varchar2(20));
end;
f、使用自定义的记录数据类型
(3)异常处理部分
a、预定义异常
exception
when 异常名称 then
异常处理;
b、自定义的异常
声明异常变量 e exception;
自己定义规则,决定什么时候抛出异常 raise e;
捕获异常
exception
when 异常名称 then
异常处理;
(1)变量声明部分
a 、 首先必须给予变量正确的名称(命名规范)
注意与SQL SERVER区分:在SQL SERVER 中自定义的变量命名以@开头
b 、赋予变量正确的数据类型
能赋予的数据类型:oracle自带的数据类型(varchar2(n),number(p,s),date)
oracle支持的数据类型(integer, string )
表名.字段名%type,表名%rowtype
记录类型(record) type record_type is record(id number(10),name varchar2(20));
异常变量类型 (exception)
c、在声明部分给变量赋予初值的两种方式: 1、:= 2、default
d、常量的使用(注意声明常量的时候就必须赋予初值,常量的值不能更改的)
(2)可执行部分
a、增、删、改语句以及"select..into .. from .."可以直接放置在begin..end之间
b、在可执行部分给变量赋予初值的两种方式: 1、 := 2、select..into .. from ..
c、dbms_output.put_line() 以及 dbms_output.put() 还有dbms_output.new_line之间的关联以及区别
d、流程控制语句
if 分支
if ... then
..
elsif .. then --注意elsif
...
else
...
end if;
--并列if
if .. then
..
end if;
if .. then
..
end if;
--嵌套if
if .. then
if .. then
...
end if;
end if;
case 分支
case
when .. then ..;
when .. then ..;
else
...
end case;
--基本循环
loop
--退出循环的两种写法
if ... then
exit;--退出
end if;
--第二种写法
exit when 条件;
end loop;
--while循环
while 条件
loop
...
end loop;
--for循环
for x in [reverse] value1..value2
loop
.....
--x := x+1; 错误的
end loop;
--注意:x是不需要声明的 value1的值一般都是小于value2,如果反向采用reverse关键字
--x的值不能够显示的修改
--goto语句 以及 null语句
--null语句最大的一个好处就是维护我们正常的语法规则
declare
x number;
y varchar2(20);
begin
x:=3;
case
when x=1 then y:=1;
when x=2 then y:=2;
when x=3 then null;
else
y:=4;
end case;
dbms_output.put_line(y);
end;
e、动态SQL语句
语法:
execute immediate '动态SQL语句'
[into ...]
[using ...]
begin
create table test(tno number,tname varchar2(20));
end;
f、使用自定义的记录数据类型
(3)异常处理部分
a、预定义异常
exception
when 异常名称 then
异常处理;
b、自定义的异常
声明异常变量 e exception;
自己定义规则,决定什么时候抛出异常 raise e;
捕获异常
exception
when 异常名称 then
异常处理;
相关文章推荐
- Oracle中PL/SQL中if语句的写法介绍
- 关于oracle中PL/SQL语句,即存储过程的写法和调用。
- Oracle(22)pl/sql编程 控制结构语句
- Oracle中使用pl/sql,查询结果可以直接修改sql语句-for update
- Oracle中PL/SQL语句的变量、常量声明和赋值
- Oracle中SQL语句连接字符串的符号使用介绍
- 《oracle pl/sql programming》 第一章 介绍 第二章 sql*plus
- ORACLE PL/SQL 中序列(sequence)的简易使用方法介绍
- Oracle PL/SQL变量、常量、流程控制、循环语句
- Oracle PL/SQL之LOOP循环控制语句
- Oracle(PL/SQL编程基础(PL/SQL程序块与PL/SQL语句)、异常、游标、数据类型)
- [oracle]pl/sql --控制语句if..
- Oracle PL/SQL之LOOP循环控制语句
- Oracle [PL/SQL]中的条件语句
- [Oracle PL/SQL]当数据表设计成具有父ID的时候,当需要选择所有与父ID同根生的所有节点的时候,使用的SQL语句
- Oracle中PL/SQL控制语句
- ORACLE PL/SQL 介绍
- Oracle PL/SQL IF语句的结构
- 在Oracle中不通过存储过程一次执行多条SQL语句Oracle PL/SQL