用存储过程写出helloword
create or replace procedure helloword as
begin
dbms_output.put_line('helloword');
end;
用存储过程写一个写一个查询结果集,使用sys_refcursor类型接受是数据结果集:
create or replace procedure EMP_LIST(P_CUR out sys_refcursor) as
begin
open P_CUR for select * from EMP;
end;
存储过程写一个输入过程:
主要强调age :=10/0赋值过程:
CREATE OR REPLACE PROCEDURE MYDEMO04
AS
age INT;
BEGIN
age :=10/0;
dbms_output.put_line(age);
EXCEPTION when others then
dbms_output.put_line('error');
END;
注意:in 是参数的默认模式,这种模式就是在程序运行的时候已经具有值,在程序体中值不会改变。
out模式定义的参数只能在过程体内部赋值,表示该参数可以将某个值传递回调用他的过程。
in out 表示高参数可以向该过程中传递值,也可以将某个值传出去
关于(if 条件 then)循环用法
create or replace procedure ADD_EMP(ename in varchar,esex in varchar,time_glob out date,hirdate in date)
is
number_one number;
begin
number_one :=10;
if number_one<20 then
begin
insert into EMP(ENAME,ESEX,TIME_GLOB,HIRDATE)values(ename,esex,time_glob,hirdate);
number_one :=number_one+1;
end;
end if;
if 30>number_one and number_one>20 then
begin
insert into EMP(ENAME,ESEX,TIME_GLOB,HIRDATE)values(ename,esex,time_glob,hirdate);
end;
end if;
exception when others then
dbms_output.put_line('数据异常');
end;
函数返回值写法:
ename in EMP.ENAME%type声明ename属性类型跟EMP.ENAME是一样的
create or replace function build(ename in EMP.ENAME%type) return number is
Result number;
esex EMP.ESEX%type;
time_glob EMP.TIME_GLOB%type;
hirdate EMP.HIRDATE%type;
number_1 number;
begin
select count(ENAME) into number_1 from EMP;
Result :=number_1*3+10;
dbms_output.put_line(Result);
return(Result);
end build;
只写了一个,这个下面为语法:
cursor%NOTFOUND
表示这个游标没有查到数据
–%FOUND
– SQL 语句影响了一行或多行时为
TRUE
–%NOTFOUND
– SQL 语句没有影响任何行时为TRUE
–%ROWCOUNT
– SQL 语句影响的行数
–%ISOPEN - 游标是否打开,始终为FALSE
create or replace procedure sys_my_cursor(rsCursor out Sys_Refcursor)
is
cursor_1 Sys_Refcursor;
Name_1 varchar(20);
begin
open cursor_1 for select ename from EMP;
LOOP
fetch cursor_1 into Name_1;
exit when
cursor_1%notfound;
end Loop;
rsCursor :=cursor_1;
close cursor_1;
close rsCursor;
end;
sys_refcursor 和 cursor 优缺点比较
优点比较
优点一:sys_refcursor,可以在存储过程中作为参数返回一个table格式的结构集(我把他认为是table类型,容易理解,其实是一个游标集), cursor 只能用在存储过程,函数,包等的实现体中,不能做参数使用。
优点二:sys_refcursor 这东西可以使用在包中做参数,进行数据库面向对象开放。哈哈。我喜欢。cursor就不能。
缺点比较:
缺点:sys_refcursor 不能用open,close ,fetch 进行操作。不好学,难理解。
857

被折叠的 条评论
为什么被折叠?



