oracle存储过程语法与使用
oracle 存储过程的基本语法 1.基本结构 1 CREATE OR REPLACE PROCEDURE 存储过程名 2 IS 3 BEGIN 4 NULL;
5 END;
行1: 3.IF 判断 4.while 循环 5.变量赋值 6.用for in 使用cursor 7.SYS_REFCURSOR 型游标,该游标是Oracle 以预先定义的游标,可作出参数进行传递 。(带参数的游标) cursor SYS_REFCURSOR; name varhcar(20); begin OPEN cursor FOR select name from student where … –SYS_REFCURSOR 只能通过OPEN 方法来打开和赋值 LOOP fetch cursor into name –SYS_REFCURSOR 只能通过fetch into 来打开和遍历 exit when cursor%NOTFOUND; –SYS_REFCURSOR 中可使用三个状态属性: —%NOTFOUND( 未找到记录信息) %FOUND( 找到记录信息) —%ROWCOUNT( 然后当前游标所指向的行位置) dbms_output.putline(name); end LOOP; rsCursor := cursor; end test; 1.在oracle中,数据表别名不能加as,如: 2.在存储过程中,select某一字段时,后面必须紧跟into,如果select整个记录,利用游标的话就另当别论了。 select af.keynode into kn from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 有into,正确编译
select af.keynode from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 没有into,编译报错,提示:Compilation
Error: PLS-00428: an INTO clause is expected in this SELECT statement
3.在利用select…into…语法时,必须先确保数据库中有该条记录,否则会报出”no data found”异常。 create or replace procedure mypro as Cursor months is select TABLE_NAME from user_tables t where t.TABLE_NAME like 'BSA_201612%';
test varchar2(30) := 'test str';
numb number :=1;
begin for mon in months loop dbms_output.put_line(test);
--select count(*) from mon into numb;
dbms_output.put_line(mon.TABLE_NAME);
end loop;
end mypro;
set serveroutput on;
exec mypro; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |