- 浏览: 10118 次
- 性别:
- 来自: 南京
文章分类
最新评论
Oracle PL/SQL 学习笔记
-- 案例 create or replace procedure sp_pro2 is begin -- 执行部分 delete from mytest where name ='name1'; end; -- 最简单的块 begin qdbms_output.put_line('hello world'); end; --- 有定义和执行部分的块 declare --定义变量 v_ename varchar2(5); v_sal number(7,2); begin -- 执行部分 select ename,sal into v_ename,v_sal from emp where empno = &no; /* select sal into v_sal from emp where empno = &no;*/ dbms_output.put_line('employeer: ' || v_ename || ' salary: ' || v_sal); exception -- 异常处理 when no_data_found then dbms_output.put_line('朋友,请重新输入编号'); end; / -- 案例4 create procedure sp_pro3(spName varchar2,newSal number) is begin --执行部分-- 根据用户名去修改工资 update emp set sal = newSal where ename =spName; end; -- 函数案例 -- 输入雇员的姓名,返回该雇员的年薪 create function sp_fun2(spName varchar2) return number is yearSal number(7,2); begin --执行部分 select sal * 12 + nvl(comm,0) * 12 into yearSal from emp where ename = spName; return yearSal; end; -- 创建包 -- 创建一个包 sp_package -- 声明该包有一个过程 -- 声明该包有一个函数 create package sp_package is procedure update_sal(name varchar2,newsal number); function annual_income(name varchar2) return number; end; -- 给包 sp_package 实现包体 create or replace package body sp_package is procedure update_sal(name varchar2,newsal number) is begin update emp set sal = newsal where ename = name; end; function annual_income(name varchar2) return number is annual_salary number; begin select sal*12+nvl(comm,0) into annual_salary from emp where ename = name; return annual_salary; end; end; -- 案例 declare c_tax_rate number(3,2):=0.03; --用户名 v_ename emp.ename%type; v_sal number(7,2); v_tax_sal number(7,2); begin --执行部分 select ename,sal into v_ename,v_sal from emp where empno=&no; --计算所得税 v_tax_sal:=v_sal*c_tax_rate; --输出 dbms_output.put_line('姓名:' || v_ename ||',工资:' || v_sal || '所得税:'|| v_tax_sal); end; --pl/sql记录实例 declare --定义一个pl/sql记录类型emp_record_type, -- 类型保护三个数据name,salary,title type emp_record_type is record( name emp.ename%type, salary emp.sal%type, title emp.job%type ); -- 定义了一个sp_record变量,这个变量类型是emp_record_type sp_record emp_record_type; begin select ename,sal,job into sp_record from emp where empno=7788; dbms_output.put_line('员工名:' || sp_record.name || ',工资是' || sp_record.salary); end; -- pl/sql 表实例 declare -- 定义一个pl/sql表类型sp_table_type,该类型是用于存放emp.ename%type -- index by binary_integer表示下标是整数 type sp_table_type is table of emp.ename%type index by binary_integer; -- 定义一个sp_table变量,这个变量的类型是sp_table_type sp_table sp_table_type; begin select ename into sp_table(0) from emp where empno=7788; dbms_output.put_line('员工名:' || sp_table(0)); end; -- pl/sql cursor实例 declare -- 定义游标类型 type sp_emp_cursor is ref cursor; -- 定义一个游标变量 test_cursor sp_emp_cursor; -- 定义变量 v_ename emp.ename%type; v_sal emp.sal%type; begin -- 执行部分 -- 把test_cursor 和一个select结合 open test_cursor for select ename,sal from emp where deptno=&no; --循环取出 loop fetch test_cursor into v_ename,v_sal; -- 判断工资高低,决定是否更新 -- 判断是否test_cursor为空 exit when test_cursor%notfound; dbms_output.put_line('名字:'||v_ename||',工资:'|| v_sal); end loop; end; -- procedure test create or replace procedure sp_pro6(spName varchar2) is --定义 v_sal emp.sal%type; begin --执行 select sal into v_sal from emp where ename=spName; --判断 if v_sal <2000 then update emp set sal = sal*1.1 where ename=spName; end if; end; -- procedure test2 create or replace procedure sp_pro6(spName varchar2) is --定义 v_comm emp.comm%type; begin --执行 select comm into v_comm from emp where ename=spName; --判断 if v_comm != 0 then update emp set comm =comm + 100 where ename=spName; else update emp set comm = comm + 200 where ename=spName; end if; end; -- procedure if elsif create or replace procedure sp_pro6(spNo number) is -- 定义 v_job emp.job%type; begin -- 执行 select job into v_job from emp where empno = spNo; if v_job = 'PRESIDENT' then update emp set sal = sal + 1000 where empno = spNo; elsif v_job = 'MANAGER' then update emp set sal = sal + 500 where empno = spNo; else update emp set sal = sal + 200 where empno = spNo; end if; end; -- loop test create or replace procedure sp_pro6(spName varchar2) is -- 定义 :=表示赋值 v_num number:=1; begin loop insert into users values(v_num,spName); -- 判断退出条件 exit when v_num = 10; -- 自增 v_num:= v_num +1; end loop; end; -- while test; create or replace procedure sp_pro6(spName varchar2) is -- 定义 :=表示赋值 v_num number:=11; begin while v_num <=20 loop -- 执行 insert into users values(v_num,spName); v_num:=v_num+1; end loop; end; -- goto test declare i int := 1; begin loop dbms_output.put_line('i=' || i); if i = 10 then goto end_loop; end if; i := i + 1; end loop; <<end_loop>> dbms_output.put_line('end loop'); end; -- 创建表 create table book( bookId number(4,2), bookName varchar2(50), publishHouse varchar2(50) ); -- 编写过程 -- in 表示输入参数, 默认为in -- out 表示输出参数 create or replace procedure sp_pro7(spBookId in number, spbookName in varchar2, sppublishHouse in varchar) is begin insert into book values(spBookId,spbookName,sppublishHouse); end; -- 有输入和输出的存储过程 create or replace procedure sp_pro8 (spNo in number,spName out varchar2, spSal out number,spJob out varchar2) is begin select ename,sal,job into spName,spSal,spJob from emp where empno = spNo; end; -- 返回结果集的过程 -- 1.创建一个包,在该包中定义一个类型test_cursor,一个游标 create or replace package testpackage as type test_cursor is ref cursor; end testpackage; -- 2. 创建存储过程 create or replace procedure sp_pro9(spNo in number, p_cursor out testpackage.test_cursor) is begin open p_cursor for select * from emp where deptno=spNo; end; -- 3. 如何在java中调用 -- oracle 的分页 select rownum rn,t1.* from (select * from emp) t1 select rownum rn,t1.* from (select * from emp) t1 where rownum < 10; -- 在分页时,大家可以把下面的sql语句当成模板使用 select * from (select rownum rn,t1.* from (select * from emp) t1 where rownum <= 10) where rn >= 6; -- 开发一个包 create or replace package testPackage as type test_cursor is ref cursor; end testPackage; -- 开始编写分页的过程 create or replace procedure fenye (tableName in varchar2, pageSize in number, -- 一页现实记录数 pageNow in number, myrows out number, -- 总记录数 myPageCount out number, -- 总页数 p_cursor out Testpackage.test_cursor -- 返回记录集 ) is -- 定义部分 -- 定义sql语句 字符串 v_sql varchar2(1024); -- 定义两个整数 v_begin number:=(pageNow-1)*pageSize + 1; v_end number:=pageNow*pageSize; begin -- 执行部分 v_sql:='select * from (select rownum rn,t1.* from (select * from '|| tableName||' order by sal) t1 where rownum <= '||v_end||') where rn >= '||v_begin||''; -- 把游标和sql语句关联起来 open p_cursor for v_sql; -- 计算myRows和myPageCount -- 组织一个SQL v_sql:='select count(*) from ' || tableName; -- 执行SQL,并把返回的值,赋给myRows execute immediate v_sql into myRows; -- 计算myPageCount if mod(myRows,pageSize)=0 then myPageCount := myRows/pageSize; else myPageCount := myRows/pageSize + 1; end if; -- 关闭游标 -- close p_cursor; end; -- 使用java测试 -- 新的需要,排序 -- 例外案例 declare -- 定义 v_name emp.ename%type; begin select ename into v_name from emp where empno = &no; dbms_output.put_line('name: '|| v_name); exception when no_data_found then dbms_output.put_line('no not exist'); end; -- 自定义例外 create or replace procedure ex_test(spNo number) is -- 定义一个例外 myex exception; begin -- 更新用户sal update emp set sal = sal + 1000 where empno= spNo; -- sql%notfound 这是表示没有update -- raise myex; 触发myex if sql%notfound then raise myex; end if; exception when myex then dbms_output.put_line('not update success'); end; -- 创建视图,把emp表薪水sal<1000 的雇员映射该视图(view) create view myview as select * from emp where sal < 1000; create view myview as select emp.empno,emp.ename,dept.dname from emp,dept where emp.deptno = dept.deptno
相关推荐
pl/sql数据库学习笔记,包含一些基础知识,和案例代码
此文档由个人总结快速学习pl/sql的案例及说明,也是快速查询pl、sql开发的精华文档,在此提供给大家学习与查阅
PL/SQL有三种集合 联合数组 嵌套表 可变数组 联合数组详解: 什么是数组?数组有什么特点 数据是线性存放的,在内存中地址是连续的 可以用索引来访问 定义联合数组? 联合数组不需要定义长度,他能容纳的元素最大...
Oracle的学习笔记,很有用的啊!里面有sqlplus,PL/SQL.对编程的朋友很有用
oracle PL-SQL 学习笔记 oracle PL-SQL 学习笔记
关于一些PL/SQL的基础知识,有些代码用到了scott用户下的默认表emp和dept;
oracle PL-SQL 学习笔记 很好的总结
这是oracle学习的进阶部分,笔记很详细,相信对大家会有帮助的!
最近学习Oracle时写的笔记,对于Oracle中常用的数据库对象、函数,常用语句,PL/SQL都有涉及到,学习的时候是看的动力节点的视频,其中部分知识点摘自网络
Oracle笔记 六、PL/SQL简单语句块、变量定义 Oracle笔记 七、PL/SQL 异常处理 Oracle笔记 八、PL/SQL跳转/判断/循环语句块 Oracle笔记 九、PL/SQL 游标的使用 Oracle笔记 十、PL/SQL存储过程 Oracle笔记 十一、...
主要是Oracle过程,函数,触发器,游标,包学习中自己所做的一些笔记,希望能够让初学者做一些参考!
剔除了oracle书上讲的多余的部分,只把重要的内容给记录下来,方便自己使用
在介绍Oracle系统管理的知识以后,将介绍与开发相关的内容(如PL/SQL基础知识、存储过程、函数、包等),并介绍数据库性能调整,《成功之路:Oracle 11g学习笔记》重点介绍SQL语句调优。SQL语句调优是《成功之路:Oracle...
成功之路 Oracle 11g 中文学习笔记 专为开发人员编写,先引导开发新手熟悉Oracle环境,然后进入数据库开发,并要掌握项目开发的一些技巧。开发技巧展示是本书一大特色(这一点很值得资深的开发人员借鉴)! 本书共24...
PL/SQL学习笔记............358 第一章 PL/SQL概述........................358 第二章 PL/SQL程序结构................359 第三章 PL/SQL数据类型................362 第四章 PL/SQL中的控制语句........368 第五章...
oracle 学习笔记 SQL 高级SQL PL/SQL的学习笔记
一点学习笔记 关于oracle pl/sql的 第一次上传多多照顾
Linux简介 Linux命令 Linux远程连接工具 Oracle在linux下的安装 Oracle相关配置 Oracle常用命令 PL/SQL连接oracle 问题总结
学习oracle知识笔记整理,包括pl/sql编程,过程、函数、游标开发等。
精通Oracle 10g PL.SQL编程电子书及学习笔记