编写第一个存储过程

create or replace procedure test_pro1 is
begin
insert into test1 values('','zydev');
end;
/

查看错误

show error

执行存储过程

exec procedure_name(value1,value2);
call procedure_name(value1,value2);

编写规范

单行注释   --

多行注释     /*.......*/

定义变量    v_name

定义常量    c_name

定义游标   name_cursor

定义例外   e_name

pl/sql块由定义部分(declear),执行部分(begin),例外处理部分(exception)三部分组成

最简单的一个块

SQL> set serveroutput on;     --打开输出选项
SQL> begin
2 dbms_output.put_line('hello,world!!!!');
3 end;
4 /

一个完整的块

declare
--定义变量
v_ename varchar2(5);
v_sal number(7,2);
begin
--执行部分,&地址符,表示从控制台接收数据
select ename,sal into v_ename,v_sal from emp where empno=&a;
--在控制台显示用户名
dbms_output.put_line('用户名是:'||v_ename||' 工资: '||v_sal);
--异常处理
exception
when no_data_found then
dbms_output.put_line('The input error, please input again!!!');
end;

 PL/SQL的复合变量

记录实例

declare
type myemp_record_type is record(name myemp.ename%type,sal myemp.sal%type,job myemp.job%type);
test_record myemp_record_type;
begin
select ename,sal,job into test_record from myemp where empno=&no;
dbms_output.put_line('姓名:'||test_record.name);
end;

表实例,相当于高级语言中的数组

declare
type myemp_table_type is table of myemp.ename%type index by binary_integer;
test_table myemp_table_type;
begin
select ename into test_table(0) from myemp where empno=7788;
dbms_output.put_line('姓名: '||test_table(0));
end;

参照变量

游标变量 -ref cursor

--输入部门号,显示所有员工工资和姓名
declare
--定义一个游标类型
type test_myemp_cursor is ref cursor;
--定义一个游标变量
test_cursor test_myemp_cursor;
--定义变量
v_name myemp.ename%type;
v_sal myemp.sal%type;
begin
--把游标变量和select结合
open test_cursor for select ename,sal from myemp where deptno=&no;
--循环取出
loop
fetch test_cursor into v_name,v_sal;
exit when test_cursor%notfound;
dbms_output.put_line('姓名: '||v_name||' 工资: '||v_sal);
end loop;
--关闭游标
close test_cursor;
end;

存储过程

输入参数

--create producure test_pro3
create or replace procedure test_pro3 (tName varchar2,newSal number) is
begin
update myemp set sal=newSal where ename=tName;
end;

 java中调用

//演示Java调用Oracle的存储过程
package com.oracle;
import java.sql.*;
public class TestOraclePro {
public static void main(String agrs[]){
try {
//1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.得到链接
Connection ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:mydev","scott","tiger");
//3.创建CallableStatement
CallableStatement cs=ct.prepareCall("{call test_pro3(?,?)}");
//4.给问号赋值
cs.setString(1, "SMITH");
cs.setInt(2, 8888);
//5.执行
cs.execute();
//6.关闭
cs.close();
ct.close();
} catch(Exception e){
e.printStackTrace();
}
}
}

 条件分支语句

create or replace procedure test_pro6(tName varchar2) is
--申明变量
v_job myemp.job%type;
--执行
begin
select job into v_job from myemp where ename=tName;
--判断
if v_job='PRESIDENT' then
update myemp set sal=sal+1000 where ename=tName;
elsif v_job='MANAGER' then
update myemp set comm=comm+500 where ename=tName;
else
update myemp set comm=comm+200 where ename=tName;
end if;
end;

循环语句

loop

--循环添加10个用户到user表中,用户编号从一开始增加
create or replace procedure test_pro7(tName varchar2) is
--定义 :=表示赋值
v_num number:=1;
begin
loop
insert into users values(v_num,tName);
exit when v_num=10;
v_num:=v_num+1;
end loop;
end;

while loop

--增加10个用户,编号从11开始
create procedure test_pro8(tName varchar2) is
v_num number:=11;
begin
while v_num<=20 loop
insert into users values(v_num,tName);
v_num:=v_num+1;
end loop;
end;

 goto语句

--goto案例,一般开发不建议使用,会破坏程序结构,使其不易维护
declare
i int :=1;
begin
loop
dbms_output.put_line('输出i='||i);
if i=10 then
--直接跳转到<<end_loop>>
goto end_loop;
end if;
i:=i+1;
end loop;
dbms_output.put_line('循环结束1');
<<end_loop>>
dbms_output.put_line('循环结束2');
end;

函数

--创建函数,输入姓名,返回年薪
create function test_fun1(tName varchar2) return number is
yearSal number(7,2);
begin
select sal*12+nvl(comm,0)*12 into yearSal from myemp where ename=tName;
return yearSal;
end;

调用函数方法

var abc number
call test_pro1('SCOTT') into:abc

包pck

--创建包,声明改包有一个存储过程和函数
create or replace package test_pck1 is
procedure update_sal(name varchar2,newsal number);
function annual_income(name varchar2) return number;
end;

创建包体

--创建包体
create or replace package body test_pck1 is
procedure update_sal(name varchar2,newsal number) is
begin
update myemp set sal=newsal where ename=name;
end;
function annual_income(name varchar2) return number is
yearSal number;
begin
select sal*12+nvl(comm,0)*12 into yearSal from myemp where ename=name;
return yearSal;
end;
end;

调用包的过程或函数

call test_pck1.update_sal('SCOTT',120);
call test_pck1.annual_income('SCOTT') into:abc

例外处理

常见的预定义例外

1. no_data_found

declare
v_name emp.ename%type;
begin
select ename into v_name from emp where empno=&no;
dbms_output.put_line('名字是:'||v_name);
exception
when no_data_found then
dbms_output.put_line('无编号');
end;

2. case_not_found

declare
v_sal myemp.sal%type;
v_num myemp.empno%type;
begin
select sal,empno into v_sal,v_num from myemp where empno=&no;
case
when v_sal<1000 then
update myemp set sal=sal+1000 where empno=v_num;
when v_sal<2000 then
update myemp set sal=sal+500 where empno=v_num;
end case;
exception
when case_not_found then
dbms_output.put_line('无匹配项');
end;

 自定义例外

create or replace procedure test_pro13(tNo number) is
--定义一个例外
myex exception;
begin
update myemp set sal=sal+100 where empno=tNo;
if sql%notfound then
raise myex;
end if;
exception
when myex then
dbms_output.put_line('没有更新用户');
end;

PL/SQL编程1-基础的更多相关文章

  1. Oracle PL/SQL编程之基础

    1.简介:pl/sql块由三个部分组成:定义部分.执行部分.例外处理部分,如下所示: declare: /*定义部分---定义常量.变量.游标.例外.复杂数据类型 begin /*执行部分---要执行 ...

  2. Oracle数据库编程:PL/SQL编程基础

    2.PL/SQL编程基础: PL/SQL块:        declare        定义部分        begin        执行部分        exception        异 ...

  3. PL/SQL编程基础(三):数据类型划分

    数据类型划分 在Oracle之中所提供的数据类型,一共分为四类: 标量类型(SCALAR,或称基本数据类型) 用于保存单个值,例如:字符串.数字.日期.布尔: 标量类型只是作为单一类型的数据存在,有的 ...

  4. 【PL/SQL编程基础】

    [PL/SQL编程基础]语法: declare 声明部分,例如定义变量.常量.游标 begin 程序编写,SQL语句 exception 处理异常 end: / 正斜杠表示执行程序快范例 -- Cre ...

  5. PL/SQL 编程(一)基础,变量,分支,循环,异常

    SQL和PL/SQL: SQL 结构化查询语言(Structural Query Language),是用来访问和操作关系型数据库的一种标准通用语言,属于第四代语言(4GL).可以方便的调用相应语句来 ...

  6. ORACLE PL/SQL编程详解

    ORACLE PL/SQL编程详解 编程详解 SQL语言只是访问.操作数据库的语言,并不是一种具有流程控制的程序设计语言,而只有程序设计语言才能用于应用软件的开发.PL /SQL是一种高级数据库程序设 ...

  7. pl/sql编程

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  8. [推荐]ORACLE PL/SQL编程详解之三:PL/SQL流程控制语句(不给规则,不成方圆)

    原文:[推荐]ORACLE PL/SQL编程详解之三:PL/SQL流程控制语句(不给规则,不成方圆) [推荐]ORACLE PL/SQL编程详解之三: PL/SQL流程控制语句(不给规则,不成方圆) ...

  9. [推荐]ORACLE PL/SQL编程详解之一:PL/SQL 程序设计简介(千里之行,始于足下)

    原文:[推荐]ORACLE PL/SQL编程详解之一:PL/SQL 程序设计简介(千里之行,始于足下) [推荐]ORACLE PL/SQL编程详解之一: PL/SQL 程序设计简介(千里之行,始于足下 ...

  10. [顶]ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之功)

    原文:[顶]ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之功) [顶]ORACLE PL/SQL编程详解之二: PL/SQL块结构和组成元素(为山九仞,岂一日 ...

随机推荐

  1. word公式编辑器公式

    Linear format equations and Math AutoCorrect in Word Applies To: Word 2016 Outlook 2016 Word 2013 Ou ...

  2. 基于jquery的json转table插件jsontotable

    分享一款基于jquery的json转table插件jsontotable.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class="container ...

  3. Android——Handler 多线程

    java 打开界面 实现图片轮播 //Handler final ImageView iv_2 = (ImageView)findViewById(R.id.iv_2); final int[] im ...

  4. HTML——动画效果回到顶层(小火箭)

    一.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  5. Elastic-Job - 分布式定时任务框架

    Elastic-Job - 分布式定时任务框架 摘要 Elastic-Job是ddframe中dd-job的作业模块中分离出来的分布式弹性作业框架.去掉了和dd-job中的监控和ddframe接入规范 ...

  6. 轻量级ORM框架Dapper应用五:使用Dapper实现Join操作

    在这篇文章中,讲解如何使用Dapper使用Inner join的操作 1.新创建两张表:Users表和Product表 Users表定义如下: CREATE TABLE [dbo].[Users]( ...

  7. 聊聊Javascript中的AOP编程

    Duck punch 我们先不谈AOP编程,先从duck punch编程谈起. 如果你去wikipedia中查找duck punch,你查阅到的应该是monkey patch这个词条.根据解释,Mon ...

  8. AJAX乱码解决新方法

    用过AJAX的朋友肯定知道javascript是使用UTF-8国际编码,即每个汉字用3个字节来存储,但是这就造成了用AJAX来send数据的时候出现乱码.     有一种解决办法就是使用encodeU ...

  9. 《FPGA全程进阶---实战演练》第二章之PCB layout注意事项以及投板几点说明

           上一篇博客讲述了各个部分的原理图,那么根据原理图画出PCB,其实PCB是一门很大的学问,想要掌握谈何容易.就笔者在画PCB时的一些注意事项做一些说明.        1.电源部分的电源线 ...

  10. hibernate、struts、spring mvc的作用

    Hibernate工作原理及为什么要用?原理:1.读取并解析配置文件2.读取并解析映射信息,创建SessionFactory3.打开Sesssion4.创建事务Transation5.持久化操作6.提 ...