首页 > 数据库 > Oracle > 正文

oracle 存储过程的基本语法

2024-08-29 13:31:41
字体:
来源:转载
供稿:网友

oracle 存储过程的基本语法


1.基本结构

create or replace procedure 存储过程名字
(
    参数1 in number,
    参数2 in number
) is
变量1 integer :=0;
变量2 date;
begin

end 存储过程名字

2.select into statement
  将select查询的结果存入到变量中,可以同时将多个列存储多个变量中,必须有一条
  记录,否则抛出异常(如果没有记录抛出no_data_found)
  例子:
  begin
  select col1,col2 into 变量1,变量2 from typestruct where xxx;
  exception
  when no_data_found then
      xxxx;
  end;
  ...

3.if 判断
  if v_test=1 then
    begin
       do something
    end;
  end if;

4.while 循环
  while v_test=1 loop
  begin
 xxxx
  end;
  end loop;

5.变量赋值
  v_test := 123;

6.用for in 使用cursor

  ...
  is
  cursor cur is select * from xxx;
  begin
 for cur_result in cur loop
  begin
   v_sum :=cur_result.列名1+cur_result.列名2
  end;
 end loop;
  end;

7.带参数的cursor
  cursor c_user(c_id number) is select name from user where typeid=c_id;
  open c_user(变量值);
  loop
 fetch c_user into v_name;
 exit fetch c_user%notfound;
    do something
  end loop;
  close c_user;

8.用pl/sql developer debug
  连接数据库后建立一个test window
  在窗口输入调用sp的代码,f9开始debug,ctrl+n单步调试

 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表