Oracle 的存储过程及游标(又学习)
Oracle 的存储过程及游标
Oracle中的存储过程和游标:
一:Oracle中的函数与存储过程的区别:
二:如何创建存储过程:
(a in int:=0,b in int:=0)
is
c int:=0;
begin
c:=a+b;
dbms_output.put_line('C is value'||c);
end;
执行:
execute myPro(10,20);
exec myPro(10,20);
(a int:=0,b int:=0)
is
c int:=0;
begin
select empno+a+b into c from emp where ename='FORD';
dbms_output.put_line('C is values '||c);
end;
执行:
execute myPro1(10,20)
(
varEmpName emp.ename%type
)
is begin
select * from emp where ename like '%'||varEmpName||'%';
end;
这个程序我们无法用into,因为在Oracle里面没有一个类型去接受一个结果集.这个时候我们可以声明游标对象去接受他.
PL/SQL游标:
(
StuID int,
StuName varchar2(20)
)
alter table 学生基本信息表 add constraint PK_STUID primary key(StuID)
declare
num int:=0;
begin
num:=#
delete from 学生基本信息表 where StuID=num;
if sql%notfound then
dbms_output.put_line('该行数据没有发现');
else
dbms_output.put_line('数据被发现并删除,影响的行数为:'||sql%rowcount);
end if;
end;
E:关于显示游标的例子:
declare
cursor mycur is select empno,ename,job from emp;
vempno emp.empno%type;
vename emp.ename%type;
vjob emp.job%type;
begin
open mycur;
fetch mycur into vempno,vename,vjob;
dbms_output.put_line('I Found You!'||mycur%rowcount||'行');
dbms_output.put_line('读取的数据为'||vempno||'
close mycur;
end;
因为只读出来一条,所以要遍历一下:
declare
cursor mycur is select empno,ename,job from emp;
vempno emp.empno%type;
vename emp.ename%type;
vjob emp.job%type;
begin
open mycur;
loop
fetch mycur into vempno,vename,vjob;
exit when mycur%notfound;
if mycur%found then
dbms_output.put_line('读取的数据为'||vempno||'
end if;
end loop;
dbms_output.put_line('I Found You!'||mycur%rowcount||'行');
close mycur;
end;
declare
cursor query(vname varchar) is select empno,ename,job from emp where ename like'%'||vname||'%';
begin
for line in query('A')
loop
dbms_output.put_line(line.empno||'
end loop;
end;
实现动态输入:
declare
cursor query(vname varchar) is select empno,ename,job from emp where ename like'%'||vname||'%';
name1 varchar(10);
begin
name1:=upper('&name1');
for line in query(name1)
loop
dbms_output.put_line(line.empno||'
end loop;
end;
posted on 2009-04-23 10:37 fengyuwuzu1980 阅读(1766) 评论(1) 编辑 收藏 举报