oracle04_plsql
PLSQL:Procedural Language SQL
(1)
plsql的基本结构(a)
declare
id constant number(2):=2;--常量定义
name varchar2(10);--变量定义
mysal emp.sal%type;--引用emp表sal列的类型
myrow emp%type; --引用emp表行类型,相当于java中对象,该行类型包含所有列
no_data exception;--异常定义
cursor c1 is select sal from emp;--游标定义,该c1中相当于java中集合,给
cursor c2(dno number) is select sal from emp where deptno=dno;---带参游标定义,该c2相当于java中带泛型集合
no_data exception;
begin
raise no_data;
exception
when no_data then xx;
when others then xx;
end;
创建存储过程语法(b):
create [or replace] PROCEDURE 过程名[(参数名 in/out 数据类型)]
AS
begin
PLSQL子程序体;
End;
或者
create [or replace] PROCEDURE 过程名[(参数名 in/out 数据类型)]
is
begin
PLSQL子程序体;
End 过程名;
(2)循环语句
第1种
for i in 1..100
loop
end loop;
第2种
while
loop
end loop;
第3种
loop
fetch c1 into xx;
exit when xx
end loop;
(3)条件判断语句
if xx then
elsif xx then xx
elsif xx then xx
else xx
end if;
(4)存储过程
第1种:不带参数
create or replace procedure mypro1 as
--这里可以声明变量
begin
end;
第2种:带参数
create or replace procedure mypro2(eno in emp.empno%type,name out varchar2) as
--这里可以声明变量
myrow emp%rowtype;
begin
select * into myrow from emp where empno=eno;
name:=myrow.ename;
end;
调用
execute mypro1;
declare
name varchar2;
begin
mypro2(7369,name);
end;
(5)存储函数
create or replace function myfn1(eno in emp.empno%type) as
return varchar2;
--这里可以声明变量
myrow emp%rowtype;
begin
select * into myrow from emp where empno=eno;
name:=myrow.ename;
return name;
end;
调用:
execute myfn1(7369);
declare
name varchar2;
begin
name:=mypro2(7369);
end;
(6)java api 调用存储过程和存储函数
DriverManager.getConncetion()---Connection---prepareCall
CallableStatement---set in 参数直接通过设置类型setInt()---registerOutParameter--
call.execute();
CallableStatement调用存储过程和存储函数的sql语句
{call mypro1(?,?)}
{?=call myfn1(?)}
jdbc:oracle:thin:@ip/数据库
jdbc:mysql://ip/数据库
(7)包头和包体
create or replace package mypackage as
type mycursor is ref cursor;--自定义游标类型
procedure mypro(eno in number,o out mycursor);
--声明存储过程,可以在变量后面加上自定义的游标类型
end mypackage;
create or replace package body mypackage as
procedure myro(eno in number,o out mycursor)
as
begin
open mycursor for select * from emp where empno=eno;
end mypro;
end mypackage;
(7)触发器
create or replace trigger 触发器名
before|after
delete|update|insert
on 表名
for each row--行级触发器
begin
end;
raise_application_error(-2999,'') 系统定义错误
oracle04_plsql的更多相关文章
随机推荐
- Fiddler中Response 的Raw乱码问题解决
有时候我们看到Response中的HTML是乱码的, 这是因为HTML被压缩了, 我们可以通过两种方法去解压缩. 1. 点击Response Raw上方的"Response body is ...
- 基于2-channel network的图片相似度判别
一.相关理论 本篇博文主要讲解2015年CVPR的一篇关于图像相似度计算的文章:<Learning to Compare Image Patches via Convolutional Neur ...
- grpc
grpc入门(二) 一. 目标 本博文要介绍的是编写 grpc入门(一)中所提到的四种rpc的方式,然后通过命令行和eclipse两种方式生成对应的java代码,关于grpc是什么东西本文不再赘述. ...
- Android WebView存在跨域访问漏洞(CNVD-2017-36682)介绍及解决
Android WebView存在跨域访问漏洞(CNVD-2017-36682).攻击者利用该漏洞,可远程获取用户隐私数据(包括手机应用数据.照片.文档等敏感信息),还可窃取用户登录凭证,在受害者毫无 ...
- python获取指定目录下的所有指定后缀的文件名
使用到的函数有: os.path.splitext():分离文件名与扩展名 os.path.splitext(file)[] 获得文件名 os.path.splitext(file)[] 获得文件扩展 ...
- 使用Linq确定序列是否包含任何元素
假设我们有一个集合,想要判断这个集合中是否包含任何元素可以使用Linq中的Any() List<string> list = new List<string> { " ...
- cookie记住浏览位置
/*返回上次浏览位置*/ $(function () { var str = window.location.href; str = str.substring(str.lastIndexOf(&qu ...
- .net的retrofit--WebApiClient库深入篇
前言 本篇文章的内容是对上一篇.net的retrofit--WebApiClient库的深层次补充,你可能需要先阅读上一篇才能理解此篇文章.本文将详细地讲解WebApiClient的原理,结合实际项目 ...
- console.log的返回值undefined
console.log(fun()); function fun(){ console.log(1); } //////输出结果为: 1 undefined console.log(fun()); ...
- JDK源码 - ArrayList
/** * ArrayList源码分析 * @author liyong * */ public class Util { @SuppressWarnings("unchecked" ...