提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
@TOCplsql入门学习记录
前言
plsql基本概念
plsql增加了sql的拓展性,可以解决更复杂的问题
提示:以下是本篇文章正文内容,下面案例可供参考
一、基本概念
变量,流程控制(包括条件语句,循环语句,顺序语句),游标,异常处理,过程,函数,包
二、案例记录
1.plsql基本框架
代码如下(示例):
过程化查询语言,解决更复杂问题,
--一、plsql基本框架
declare
--定义区
begin
--执行区
exception
--异常处理区
end;
2. 变量
变量是存放单个数据的容器
变量需要先定义再使用
使用变量实际是使用变量中的值
--2.1变量的类型
--基本数据类型 varchar2 number date
--记录数据类型 %type %rowtype record
declare
v_name employees.last_name%type;
v_emp employees%rowtype;
type my_type is record(
v_name employees.last_name%type,
v_sal employees.salary%type
);
v_my_emp my_type;
begin
--2.2.变量的赋值
--1.直接赋值
v_id number:=50;
--2.&input 脚本赋值
v_name :=&input;
--3.into
select * into v_emp from employees where employee_id=100;
end;
4. 流程控制语句
--4.1条件语句
--单分支条件语句
if 条件成立 then
执行体
end if;
--双分支条件语句
if 条件成立 then
执行体1
else
执行体2
end if;
--多分支条件语句
if 条件1 then
执行体1
elsif 条件2 then
执行体2
else
执行体3
end if;
--4.2循环
--简单循环loop
条件初始化语句
loop
执行体语句
exit when ...条件判断语句
条件控制语句num=num+1;
end loop;
--while 循环
while 条件成立 loop
--执行体语句
end loop;
--for 也叫次数限制循环
for i in 下限 .. 上限 loop
end loop;
--4.3顺序语句
<<flag>>
goto flag
--goto 到标记的地方一定要有执行的东西
--案例:输入一个数据num1,输入num2,输出num1到num2之间所有数
declare
num1 number:=&input;
num2 number:=&input;
begin
for i in num1 .. num2 loop
if
mod(i,2)=0 then
dbms_output.put_line(i);
end if;
end loop;
end;
--4.4 case when then end 返回一个结果
--输出指定员工的信息
declare
v_name varchar2(20):=&input;
v_emp employees%rowtype;
begin
select * into v_emp
from employees where last_name=v_name;
end;
5. 游标
–五、游标是处理查询语句返回多行记录
–指向上下文的一个指针或句柄
–显式游标:处理多行或单行查询语句
–隐式游标 sql:处理单行查询语句,delete update insert
–隐式游标不需要定义,打开,抓取,关闭等操作
–属性:
– %found :上一次成功抓取
– %notfound:上一次没有数据返回
– %rowcount:当前游标所在行数
– %isopen:游标是否打开
declare
cursor emp_cursor is
select * from employees;
begin
打开游标
open emp_cursor
抓取游标
fetch emp_cursor
while emp_cursor%found loop
dbms_output.put_line(v_emp.last_name);
end loop;
end;
--游标中可以定义参数,并且可以使用默认值
--获取指定部门的员工信息
declare
cursor emp_cursor(v_dept number default 50) is
select last_name from employees where department_id=v_dept;
begin
for i in emp_cursor(20) loop
dbms_output.put_line(i.last_name);
end loop;
dbms_output.put_line('-------------------------------');
for i in emp_cursor(50) loop
dbms_output.put_line(i.last_name);
end loop;
end;
--ref 动态游标
declare
type emp_ref is ref cursor;--定义游标类型
c1 emp_ref;--
begin
open 的时候关联业务
open c1 for select * from employees;
open c1 for select * from departments;
--不用以后关闭游标
close c1;
end;
异常处理
--异常
--输出指定员工的信息
declare
v_emp employees%rowtype;
v_id number:=&input;
begin
select * into v_emp from employees where employee_id=v_id;
dbms_output.put_line(v_emp.last_name);
exception
when no_data_found then
dbms_output.put_line('查无此人');
end;
--预定义异常:大约24种
--非预定义异常:有标准的错误码,需要用户定义名字和关联
no_result exception;
pragma exception_init(no_result,sqlcode);
--自定义异常:系统不报错,用户觉得可能和预期结果不同,定义异常
no_result exception
if sql%notfound then
raise no_result;
end if;
函数和存储过程
–存储函数 和存储过程
–函数有返回值,存储过程不返回值
select substr(‘helloworld’,1,1);
–单行函数
–字符函数
–数字函数
–日期函数
–转换函数
–通用函数
–聚合函数
–开窗函数
–函数
–参数:外部传递数据的入口
–要不要参数取决于函数在计算的过程中需不需要外部传递值进去
–in 外部传值到函数中
–out 函数中的值可以带到外部使用,
–解决函数只有一个返回值,存储过程没有返回值的问题
create or replace function(
v_id number)
return number
is
--定义变量
begin
--执行体
return
end;
create or replace procedure(v_id number,v_name out varchar2)
is
--定义变量
begin
--执行体
return
end;
--调试
--参数传递
--位置传递
--名字传递
--混合传递
包
–包:一组由相关过程、参数
–包定义(包头)package
–package body(包体)
总结
记录了plsql的入门基本知识,介绍了基本的sql语言编写