ORACLE存储过程(一)
———————————————— Hello World ——————————————————————
create or replace procedure HelloWorld ASbegin insert into MYPROCEDURE(id,name,city,MANAGER)values(1,'ss','ss','ss');end HelloWorld;
存储过程一般分分三部分:声明部分、执行部分、异常部分
变量声明:
语法 变量名+变量类型
create or replace procedure HelloWorld ASvariable_a varchar(8);variable_b varchar(5);begin insert into MYPROCEDURE(id,name,city,MANAGER)values(1,'ss','ss','ss');end HelloWorld;
if判断:
语法:if (表达式) then begin end;end if;
create or replace procedure test(prameter in number) is begin if x > 0 then begin x := 0 - x; end; end if; if x = 0 then begin x := 1; end; end if;end test;
for循环遍历:
语法:for ... in ... loop
(1)循环遍历游标
create or replace procedure test() as Cursor cursorName is select table from student; name varchar(20);begin for name in cursorName LOOP begin dbms_output.putline(name); end; end LOOP;end test;
(2)循环遍历数组
create or replace procedure test(array in TestArray) as i number;begin i:=1; for i in TestArray LOOP dbms_output.put_line(array(i));end test;
while循环遍历:
语法:while(表达式)loop
create or replace procedure test(i in number) asbegin while i < 10 LOOP begin i := i + 1; end; end LOOP; dbms_output.put_line(i);end test;
大概的知道存储过程是怎么个情况,接下来通过实例逐步了解存储过程
建表:
create table MYPROCEDURE( ID NUMBER not null, NAME VARCHAR2(20), CITY VARCHAR2(20), MANAGER VARCHAR2(20))
存储过程:
create or replace procedure HelloWorld ASbegin insert into MYPROCEDURE(id,name,city,MANAGER)values(1,'ss','ss','ss');end HelloWorld;
存储过程的调用:
(1)PL/SQL匿名块调用