oracle存储过程(基础)

1.创建

create or replace procedure mydemo01  --mydemo01存储过程名
as
begin
	dbms_output.put_line('hello word')   --打印出 hello word
end;

注:

视图中只能使用 as 不能使用 is
游标中只能使用 is 不能使用 as

2.调用

2.1 声明(declare)关键字

declare
begin
	mydemo01;
end;

2.2不使用声明(declare)关键字

begin
	mydemo01; 	--此处也可以mydemo01();完成存储过程的调用
end;

2.3使用call

call mydemo01();  --完成调用 ;注意括号不能少

3.带有参数的存储过程

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

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

4.in,out参数问题

create or replace procedure myDemo03(name out varchar,age in int)
as
begin
		dbms_output.put_line('age = '||age);
		select 'ex_sunqi' 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

5.循环

5.1 for 循环

create or replace procedure myDemo04
as 
begin 
	FOR USER in (select * from T_USER_INFO) lopp
			if (USER.id<3) then
				dbms_output.put_line(USER.USER_NAME);
			end if;
	end loop;
end;

CALL myDemo04();

执行结果如下:
请添加图片描述

5.2 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;

CALL myDemo05();

执行结果如下:
请添加图片描述

6.稍微复杂一点的存储过程

首先创建一张表:

-- Create table
create table CLASSES
(
  id       NUMBER not null,
  name     VARCHAR2(14),
  classesc VARCHAR2(10),
  seq      NUMBER(5)
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints
alter table CLASSES
  add constraint PK_CLASSES primary key (ID)
  using index
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

创建序列:

-- Create sequence
create sequence SEQ_CLASSES
minvalue 1
maxvalue 9999999999999999999999999999
start with 2
increment by 1
cache 20;

创建存储过程:

create or replace procedure proclasses(Names     in varchar2,
                                       classescs in varchar) as
/*在我们创建存储过程的时候as其实是is*/
  id  number;/*设置变量名称*/
  c   number;
  seq number;
begin
  select SEQ_CLASSES.nextval into id from dual;/*获取下一个序列,使用into赋值给id这个变量名称*/
  dbms_output.put_line('classescs=' || classescs);/*打印而已*/
  select count(*) into c from Classes where classesc = classescs;/*条件判断,classesc=进来的变量*/
  if (c > 0) then/*当数量大于0时*/
    select max(seq) + 1 into seq from Classes where classesc = classescs;
    dbms_output.put_line('第一个seq' || seq);
  else
    if (c = 0) then
      seq := 0;/*如果查询出来的数量为0的时候,我们赋值seq变量为0*/
      dbms_output.put_line('c=0的时候seq' || seq);
    end if;
  end if;
  insert into classes
    (id, name, classesc, seq)
  values
    (id, names, classescs, seq);
 /*insert插入这个不用多说了,大家都明白;注意的是我们insert之后一定要提交。
  不然数据没有持久化到数据库,这个insert没有任何意义了*/
end proclasses;

调用:

-- Created on 2019/1/7 星期一 by ADMINISTRATOR
declare
  -- Local variables here
  names varchar2(32):='晓明';
  classescs varchar2(32):='一班';
begin
  -- Test statements here
  proclasses(names,classescs);
end;
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值