PL/SQL小例子

最近学习了Oraclec的PL/SQL,以下是写的一些小例子,贴出来分享


--过程定义
create or replace procedure proc1(var_month in test.month%type) is
v_sal test.sal%type;
begin
select sal into v_sal from test where month=var_month;
if v_sal > 4000 then
begin
dbms_output.put_line(v_sal);
end;
end if;
end;


--函数定义
create or replace function func1(var_month test.month%type)
return test.sal%type
is v_sal test.sal%type;
begin
select sal into v_sal from test where month=var_month;
return v_sal;
end;

--函数的调用
declare
v_sal test.sal%type;
begin
v_sal:=func1(7);
dbms_output.put_line(v_sal);
end;

--带有复合变量的存储过程
create or replace procedure proc2
as
type emp_record is record
(
name emp.ename%type,
salary emp.sal%type,
dno emp.deptno%type
);
user_record emp_record;
begin
select ename,sal,deptno into user_record from emp where empno=7788;
dbms_output.put_line('用户名'||user_record.name||', 薪水'||user_record.salary||', 部门编号'||user_record.dno);
end;


--定义一个包,包由两部分组成,包规范和包体
create or replace package mypackage is
procedure myproc(varempno in emp.empno%type);
function myfunc(varempno in emp.empno%type) return emp.sal%type;
end mypackage;

--下面是包体
create or replace package body mypackage is
procedure myproc(varempno in emp.empno%type)
as
v_sal emp.sal%type;
begin
select sal into v_sal from emp where empno=varempno;
dbms_output.put_line(v_sal);
end;

function myfunc(varempno in emp.empno%type)
return emp.sal%type
as
v_sal2 emp.sal%type;
begin
select sal into v_sal2 from emp where empno=''||varempno||'';
return v_sal2;
end;
end mypackage;

--调用包中的过程和方法
declare
v_sal emp.sal%type;
begin
mypackage.myproc(7788);
v_sal:=mypackage.myfunc(7788);
dbms_output.put_line(v_sal);
end;

--定义复合类型PL/SQL表实例,其实就是一个特殊数组
--index by binary_integer表示下表是整数
declare
type emp_table_type is table of emp.ename%type index by binary_integer;
table_ename emp_table_type;
begin
select ename into table_ename(0) from emp where empno=7788;
dbms_output.put_line('员工名:'||table_ename(0));
end;

--游标的使用

declare
type emp_cursor is ref cursor;
v_sal emp.sal%type;
v_ename emp.ename%type;
test_cursor emp_cursor;
begin
open test_cursor for select ename,sal from emp where deptno=&deptnum;
loop
fetch test_cursor into v_ename,v_sal;
exit when test_cursor%NOTFOUND;
if v_sal < 2000 then
update emp set sal=2000 where ename=v_ename and deptno=&deptnum;
end if;
dbms_output.put_line('姓名:'||v_ename||' 工资为:'||v_sal);
end loop;
close test_cursor;
end;


--通过存储过程来使用游标和复合变量
create or replace procedure proc3(num in emp.deptno%type) as
type emp_type is record
(
v_ename emp.ename%type,
v_sal emp.sal%type
);
test_type emp_type;
cursor mycur is
select ename,sal from emp where deptno=num;
begin
if mycur%ISOPEN = false then
open mycur;
end if;
loop
fetch mycur into test_type;
exit when mycur%NOTFOUND;
dbms_output.put_line('用户名:'||test_type.v_ename||' 工资:'||test_type.v_sal);
end loop;
close mycur;
end;


--创建一个带有返回值的存储过程

create or replace procedure proc5(empnum in emp.empno%type,empsal out emp.sal%type,empjob out emp.job%type) is
begin
select sal,job into empsal,empjob from emp where empno=empnum;
end;

--定义一个包,并在包中定义游标以便能在存储过程参数中得到结果集
create or replace package testpackage as
type emp_cursor is ref cursor;
end testpackage;

--定义一个带有cursor类型的存储过程
create or replace procedure proc6(deptnum in number,test_cursor out testpackage.emp_cursor) is
begin
open test_cursor for select * from emp where deptno=deptnum;
end;

--分页语句
select * from (select A.*,rownum rn from (select * from emp) A) where rn>=5 and rn<=10;


--定义一个包,并在包中定义一个取得分页结果集的游标
create or replace package fypackage as
type fenye_cursor is ref cursor;
end fypackage;

--定义一个存储过程用来表示分页
create or replace procedure fenye(
tableName in varchar2,--查询的表名
myPageSize in number,--每页显示记录条数
pageNum in number,--第几页
allRows out number,--所有记录条数
allPages out number,--所有页数
result_cursor out fypackage.fenye_cursor--返回结果集游标
) is
v_sql varchar2(1000);
v_start number:=(pageNum-1)*myPageSize+1;
v_end number:=pageNum*myPageSize;
begin
v_sql:='select * from (select A.*,rownum rn from (select * from '||tableName||') A) where rn>='||v_start||' and rn<='||v_end;
open result_cursor for v_sql;
v_sql:='select count(*) from '||tableName;
execute immediate v_sql into allRows;
if mod(allRows,myPageSize)=0 then
allPages:=allRows/myPageSize;
else
allPages:=allRows/myPageSize+1;
end if;
end;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PL/SQL编程 pl/sql(procedural language/sql)是Oracle在标准的sql语言上的扩展。pl/sql不仅允许嵌入式sql语言,还可以定义变量和常量,允许使用条件语句和循环语句,允许使用例外处理各种错误。这样使得他的功能变的更强大。缺点是移植性不好。 编写一个存储过程,向表中添加数据。 1. create table mytest (name varchar2(30),passwd varchar2(30)); 2. create or replace procedure xxc_pro1 is begin insert into mytest values ('小红','m123'); end; 3. 调用过程 exec 过程名(参数1,参数2…)或call 过程名参数1,参数2…) ① exec xxc_pro1; 或者是 ② call xxc_pro1; pl/sql可以做什么? 块:包括过程、函数、触发器、包。 编写规范: 1. 注释 --:单行注释 eg:select * from emp where empno=7788;--取得员工信息 /*……*/多行注释 2. 表示符号(变量)的命名规范: ① 当定义变量时,建议用v_作为前缀:v_ename ② 当定义常量时,建议用c_作为前缀:c_rate ③ 当定义游标时,建议用_cursor作为后缀:emp_cursor ④ 当定义例外时,建议用e_作为前缀:e_error 块(block)是pl/sql的今本程序单元,编写pl/sql程序实际上就是在编写pl/sql块;pl/sql块由三部分组成:定义部分,执行部分,例外处理部分。 declare --可选部分 /*定义部分:定义常量,变量,游标,例外,复杂数据类型*/ begin --必选部分 /*执行部分:要执行的pl/sql语句和sql语句*/ exception --可选部分 /*例外处理部分:处理运行的各种错误*/ 实例1:只包含执行部分的pl/sqlSQL> set serveroutput on --打开输出 SQL> begin 2 dbms_output.put_line('hello'); 3 end; 4 / 说明:dbms_output是oracle提供的包,该包包含一些过程,put_line就是其中之一。 实例2:包含定义部分和执行部分 SQL> declare 2 v_ename varchar2(5); 3 begin 4 select ename into v_ename from emp where empno = &no; 5 dbms_output.put_line('雇员名'||v_ename); 6 end; 7 / 说明:&:从控制台输入变量,会弹出一个对话框。 实例3.同时输出雇员名和工资 SQL> declare 2 v_ename varchar2(20); 3 v_sal number(10,2); 4 begin 5 select ename,sal into v_ename,v_sal from emp where empno=&no; 6 dbms_output.put_line('雇员名:'||v_ename||' 工资:'||v_sal); 7 end; 8 / 包含定义,执行,和例外处理的pl/sql块。 实例4.当输入的员工号不存在时 SQL> declare 2 v_ename varchar2(20); 3 v_sal number(10,2); 4 begin 5 select ename,sal into v_ename,v_sal from emp where empno =&no; 6 dbms_output.put_line('雇员名:'||v_ename||' 工资:'||v_sal); 7 exception --异常处理部分 8 when no_data_found then 9 dbms_output.put_line('请输入正确的员工号!'); 10 end; 11 / 以上为块的基础,下面来介绍块的各个组成:过程,函数,触发器,包。 过程 过程用于执行特定的操作,当执行过程的时候,可以指定输入参数(in),也可以指定输出参数(out)。通过在过程中使用输入参数,可以讲数据输入到执行部分,通过使用输出参数,可以将执行部分的数据输出到应用环境,在pl/sql中可以使用create procedure命令来创建过程。 编写一个存储过程,可以输入雇员名和新工资来改变员工工资。 --案例 create or replace procedure xxc_pro3(newname in varchar2,newsal in number) is begin update emp set sal=newsal where ename=newname; end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值