PL/SQL
简介
PL/SQL 是过程语言(Procedural Language)与结构化查询语言(SQL)结合而成的编程语言。PL/SQL 是对 SQL 的扩展。支持多种数据类型,如大对象和集合类型,可使用条件和循环等控制结构。可用于创建存储过程、函数、触发器和程序包,给SQL语句的执行添加程序逻辑。
作用: 如果出现需要插入1000条数据,这些数据带有某种规律性,就可以通过PL/SQL进行增加数据的操作优点:
支持 SQL,在 PL/SQL 中可以使用:
数据操纵命令:DML,DDL(需要动态执行)事务控制命令:TCL游标控制, 下一章学习SQL函数和SQL运算符与 SQL 紧密集成,简化数据处理。
支持所有 SQL 数据类型支持 NULL 值支持 %TYPE 和 %ROWTYPE
%TYPE: 引用字段类型,如:employee.ename%TYPE%ROWTYPE:引用表的行类型, 如: employee%ROWTYPE支持面向对象编程 (OOP)可移植性,可运行在任何操作系统和平台上的Oralce 数据库更佳的性能,PL/SQL 经过编译执行
常用数据类型
数值 number, numeric, decimal, float, int, integer, real字符串 varchar,varchar2,string(PLSQL特有的)布尔类型boolean日期常用类型 date, timestamp属性类型 %type:引用某一个变量的类型或都是某个列名(字段)的类型。employee.ename%type; %rowtype:表示可存储某表行的记录类型。Employee%rowtype;
变量声明以及赋值
复制
$变量名$ [
CONSTANT] $datatype$ [
NOT NULL] [:= value];
$变量名$ [
CONSTANT] $datatype$ [
NOT NULL] [
DEFAULT expr];
age int;
age int :=
18;
age int
default 18;
age
constant int :=
18;
myname employee.ename%
type;
rec_emp employee%rowtype;
declare
v_empno employee.empno%
type;
rec_emp employee%rowtype;
begin
v_empno := &请输入员工编号;
select * into rec_emp from employee where empno=v_empno;
dbms_output.put_line(rec_emp.empno||','
||rec_emp.ename||','
||rec_emp.sal||','
||rec_emp.job);
end;
/
type TYPE_EMP_REC
is record(
empno employee.empno%
type,
ename employee.ename%
type,
sal employee.sal%
type,
job employee.job%
type
);
select ename into myname from employee where empno=
7879;
select ename,sal into myname,mysal from employee where empno =
7879;
基本单元框架
复制[
DECLARE
]
BEGIN
[
EXCEPTION
]
END;
/
输出输入语句
输出
oracle内置有一个dbms_output对象,负责输出 dbms_output对象有两个输出方法,一个为put,另外一个外put_line参数可以接收数值型
复制
dbms_output.put(
'hello world');
dbms_output.put_line(
'');
dbms_output.new_line;
dbms_output.put_line(
'hello world');
如果是在命令行窗口的话,没有见到输出语句,需要设置开启输出
复制
set serveroutput
on;
输入
复制--&号在PL/SQL中作为一个替代变量,用于在运行中输入值。如果输入的是字符类型那把&号放在两个单引号中,
--若是数字则不用单引号括起来
v_ename := '&请输入姓名:';
v_sal := &请输入工资;
输入输出例子
复制
declare
v_name varchar2(
10) :=
'&输入名字';
v_age int := &输入年龄;
begin
dbms_out.put_line(
'我是'||v_name||
'年龄为'||v_age);
end;
/
判断与分支语句
if判断
复制
if 表达式
then
end if
if-else判断
复制
if 表达式
then
else
end if
if-else if判断
复制
if 表达式
then
else if 表达式
then
else
end if
end if
例子:
复制--出现几个
if就得出现几个
end if
declare
v_sal employee.sal%
type := &请输入工资;
begin
if v_sal >=
5000 then
dbms_output.put(
'经理');
else if v_sal<
5000 and v_sal >=
3000 then
dbms_output.put(
'员工');
else if v_sal<
3000 and v_sal>
1000 then
dbms_output.put(
'other');
else
dbms_output.put(
'others');
end if;
end if;
end if;
dbms_output.new_line;
end;
/
--少了elsle
declare
v_sal employee.sal%
type := &请输入工资;
begin
if v_sal >=
5000 then
dbms_output.put(
'经理');
else if v_sal<
5000 and v_sal >=
3000 then
dbms_output.put(
'员工');
else if v_sal<
3000 and v_sal>
1000 then
dbms_output.put(
'other');
end if;
end if;
end if;
dbms_output.new_line;
end;
/
case分支语句
复制
case
when 表达式
then
when 表达式
then
else
end case;
case
when 数值
then
when 数值
then
else
end case;
数值不能能为字符串类型
循环语句for while loop
loop循环
复制
loop
IF 条件
THEN
EXIT;
END IF;
end loop;
while循环
复制
while 条件
LOOP
EXIT WHEN 条件;
end loop;
for循环
复制
for i in 1..10 loop
end loop;
for i in reverse 1..10 loop
end loop;
-使用变量作为for循环的范围
declare
v_min
integer default 1;
v_max integer default 20;
begin
for i
in reverse v_min..v_max
loop
dbms_output.put(i||
',');
end loop;
dbms_output.new_line;
end;
/
循环结构练习
复制--遍历输出某个部门的全部员工信息
declare
v_deptno
integer := &请输入部门号;
begin
for emp
in (
select e.*
from employee e
where deptno=v_deptno)
loop
dbms_output.put_line(||emp.empno||
end loop;
end;
/
--
for打印
99乘法表
begin
--如果不在rownum,可以吗?
for num1
in 1..9 loop
for num2
in 1..num1
loop
dbms_output.put(num2||
end loop;
dbms_output.new_line;
end loop;
end;
/
--
loop打印
99乘法表
declare
num1 int :=
1;
num2 int;
begin
loop
num2 :=
1;
loop
dbms_output.put(num2||
exit when num1=num2;
num2 := num2+
1;
end loop;
dbms_output.new_line;
exit when num1=
9;
num1 := num1+
1;
end loop;
end;
/
--
while打印
99乘法表
declare
num1 int :=
1;
num2 int;
begin
while num1 <=
9 loop
num2 :=
1;
while num2 <= num1
loop
dbms_output.put(num2||
exit when num2=num1;
num2 :=num2+
1;
end loop;
dbms_output.new_line;
exit when num1=
9;
num1 := num1+
1;
end loop;
end;
/
转载于:https://www.cnblogs.com/chaoyang123/p/11549427.html
相关资源:JAVA上百实例源码以及开源项目