Oracle 存储过程

oracle 存储过程
declare
第一种

create or replace procedure myDemo01
as
begin
  dbms_output.put_line('hello word, my name is stored procedure');
end;
create or replace procedure: 关键字用来创建或覆盖一个原有的存储过程
as: 关键字。
注:
	在存储过程(procedure)和函数(function)中没有区别;
	在视图(VIEW)中只能用AS不能用IS。
	在游标(cursor)中只能用is 不能用AS。
begin: 关键字。
dbms_output.put_line('helloworld'); : 输出内容
end;关键字。

第二种

create or replace procedure demo2
as
name varchar2(10); -- 声明变量,注意varchar需要指定长度
age int;
begin
	name:='xiaoming';-- 变量赋值
	age:=18;
	dbms_output.put_line('name='||name||', age='||age);--通过||符号达到连接字符串的功能
end;

Oracle存储过程给变量赋值的方法
1.直接法————:=
例如 v_count := 0;
2.select into
例如: 假设变量名为 v_count , select count(1) into v_count from dual;
3.execute immediate 变量名(一般是sql的select 语句)into变量名
例如:v_name := 'select name from student where code='||code||' and status =1;'
其中code是查询条件

注:用sql时尽量用引号
/
注意:需要在存储过程输入完成后回车,下一行输入“/”回车,才会创建成功。
当提示Procedurecreated 表示存储过程创建成功

  • cursor 定义结果时需要注意与begin中用做条件时,不能查出为空。不然会报异常。

  • drop procedure sp_name;删除存储过程

执行存储过程
declare
begin
    存储过程名字
end;
posted @ 2021-03-04 11:07  ^_^达❤令  阅读(249)  评论(0编辑  收藏  举报