Oracle存储过程

Oracle 存储过程

1. 创建存储过程语法

create or replace procedure 存储过程名
as
begin
  ----------------------------
end;

-- 注:
  -- 在存储过程(PROCEDURE)和函数(FUNCTION)中没有区别;
  -- 在视图(VIEW)中只能用AS不能用IS;
  -- 在游标(CURSOR)中只能用IS不能用AS。

2. 输出案例

create or replace procedure myDemo01
as 	  -- 关键字
begin -- 关键字
  dbms_output.put_line('hello word, my name is stored procedure');--  输出内容
end;  -- 关键字

3. 调用存储过程
-- 方式一:声明 declare 关键字
declare
begin 
 myDemo01;
end;
-- 方式二:不声明 declare 关键字
begin
 myDemo01; -- 在此处也可使用 myDemo01();完成存储过程的调用
end;
-- 方式三:call
call myDemo01(); -- call 存储过程名 可完成调用,注意括号不能少

4. 带有参数的存储过程

create or replace procedure myDemo02(name in varchar,age in int)
as
begin
  dbms_output.put_line('name='||name||', age='||age);
end;

-- 注:
-- 		在调用存储过程时,如果存储过程没有参数,调用时括号()可以不带

5. in,out 参数问题

create or replace procedure myDemo03(name out varchar,age in int)
as
begin
    dbms_output.put_line('age='||age);
    select 'ex_zzz' into name from dual;
end;

-- 执行调用操作
declare
   name varchar(10);
   age int;
begin
   myDemo03(name=>name,age=>25);
   dbms_output.put_line('name='||name);
end;
-- 注:
	-- in 表示输入,out 用于输出,参数默认类型是 in 类型

6. 异常写法

create or replace procedure myDemo04
as
age int;
begin
  age:=1/0;
  dbms_output.put_line(age);
  --异常
  exception when others then
    dbms_output.put_line('error');
end;
-- 执行调用操作
call myDemo04();


7. 循环

-- 1.while 循环
create or replace procedure myDemo05
as
  n_count number := 0;
begin
  while n_count < 5 loop
    dbms_output.put_line(n_count);
    n_count := n_count + 1;
  end loop;
end;
-- 调用存储过程操作
begin
  myDemo05;
end;

-- 2.for 循环
-- 数据准备
CREATE TABLE student1(sids number(11) NOT NULL,
                      sname varchar(10),
                      score float,
                      sex char(1),
                      sage number(10),
                      addr varchar(10));

INSERT INTO student1 VALUES (101, 'zhang', 98, '2', 15, '苏州');
INSERT INTO student1 VALUES (102, 'han', 69, '1', 30, '西安');
INSERT INTO student1 VALUES (103, 'hui', 72, '1', 25, '苏州');
INSERT INTO student1 VALUES (104, 'fang', 100, '2', 35, '苏州');
INSERT INTO student1 VALUES (105, 'li', 88, '1', 45, '北京');
INSERT INTO student1 VALUES (106, 'cheng', NULL, '1', 12, '北京');
INSERT INTO student1 VALUES (107, 'zhao', NULL, '3', 23, '北京');
-- 存储过程-循环遍历
create or replace procedure myDemo06
as
begin
 FOR USE in (select * from student1) loop
     if (USE.sids<108) then
        dbms_output.put_line(USE.USER_NAME);
      end if;
  end loop;
end;

-- 执行调用操作
CALL myDemo06();

8. 基本增删改查

create or replace procedure mydemo07(sids in int, sname in varchar,score in int, sex in char,sage in int,addr in varchar)
as
begin
     insert into student1 VALUES(sids,sname,score,sex,sage,addr);
     --UPDATE  student1 t SET t.sname='aaa' WHERE t.sids=sids ;
     --DELETE  student1 t WHERE t.sids=sids ; 
    commit; --提交
end MyDemo07;

begin
mydemo07(107, '测试', 100, '1', 27, '北京');
end;


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值