存储过程

2018-09-03  本文已影响0人  _拾年丶

存储过程

过程,可以简单理解为是一个函数。

函数和过程的区别:函数总是向调用者返回数据,而过程没有数据返回。

Oracle 可以将 PL/SQL 代码存储在数据库中,然后可以提供给其他地方来运行它。
这个动作就叫存储过程,也可以理解为一个创建一个函数给其他人用。

语法格式
procedure 名字(参数)
is
声明局部变量;
begin
语句
end;

也可以使用图形界面方式创建:文件 → 新建 → 程序窗口 → procedure


往 tb_stuu 表中插入一条数据
create procedure hello
is
begin
  insert into tb_stuu(id, name)
  values (5, '小明');
  commit; -- 提交数据
end;

-- 使用
begin
 hello();
end;

select * from tb_stuu;
-- 带参数的
create procedure hello2(vid tb_stuu.id%type, vname tb_stuu.name%type)
is
begin
  insert into tb_stuu(id, name)
  values (vid, vname);
  commit; -- 提交数据
end;

-- 使用
begin
 hello2(6, '大明');
end;

-- 让 id 自动增长,使用序列
begin
 hello2(stu_seq.nextval, '大大明');
end;
上一篇 下一篇

猜你喜欢

热点阅读