300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 存储过程+调用存储过程+无/带参的存储过程+in参数+out参数+int out参数+为参数设置默认值...

存储过程+调用存储过程+无/带参的存储过程+in参数+out参数+int out参数+为参数设置默认值...

时间:2023-12-27 15:08:51

相关推荐

存储过程+调用存储过程+无/带参的存储过程+in参数+out参数+int out参数+为参数设置默认值...

存储过程

1存储过程是一组为了完成特定功能的SQL语句集

2存储过程编译后存储在数据库中

3执行存储过程比执行存储过程封装的SQl语句集更有效率

4不能指定declare关键字

5: OUT 和 IN OUT 的形参不能有默认表达式,只有in参数可以设置默认值

语法

create 【or replace】procedure procedure_name

【(parameter 【in | out | int out】data_type 【default value】)【,….】】

{is | as }

【declaration_section;】

begin

procedure _body;

end【procedure_name】;

解释

default value:为参数设置默认值,只适用于in参数

or replace:替换已经存在的存储过程

procedure_name:存储过程名称

parameter :参数名

in :传递参数

out:返回一个参数

int out:传递和返回一个参数

data_type:参数的数据类型,用于过程体重

is | as:用于声明变量

declaration_section:变量名称

调用存储过程

1创建存储过程后,过程体中的内容没有被执行,仅仅只是被编译。要想执行过程体中的内容,还需调用存储过程。

语法

call procedure_name(【parameter 【,….】】);

或者

exec【UTE】 procedure_name(【parameter 【,….】】);

举例

无参的简单存储过程

create or replace procedure insert_student2

as

begin

insert into student2 values(‘2’,’lilib’,18,’1’);

end insert_student2;

查看前后数据的变化

in参数

1in参数是指输入参数,由调用者为其赋值,也可以使用默认值,如果不为参数指定模式,默认in.

2在调用in参数的存储过程时,为参数参数赋值的形式有2种

1)不指定参数名,只提供参数值,oracle会按存储过程中参数的先后顺序为参数赋值,需要保证值的个数与类型和参数的个数与类型相匹配。

2)指定参数名:这种情况可以不考虑参数的顺序。procedure_name=>value.

存储过程代码

create or replace procedure update_student2(no in varchar2,name in varchar2)

as

begin

update student2 set sname=name where sno=no;

end update_student2;

不指定参数名的存储过程

call update_student2(‘1’,’aaa’);

指定参数名的存储过程

call update_student2(name=>’bbb’,no=>’2’);

out参数

1out参数是指输出参数,由存储过程的语句为其赋值,返回给用户

2如果用户需要获取存储过程out参数的返回值,需要使用variable语句声明变量接收返回值,并在调用过程的时候绑定变量.

variable语法

variable variable_name datatype;–声明变量

【,….】

exec【ute】 procedure_name(:variable_name 【,….】);–调用过程的时候绑定变量

案例

create or replace procedure select_student2(no in varchar2,name out varchar2)

as

begin

select sname into name from student2 where sno=no;

end select_student2;

variable stu_name varchar2(20)

exec select_student2(‘1’,:stu_name);

print stu_name;

int out参数

1同时拥有int和out的特性

2既可以接收用户的值,有允许存储过程中修改其值,并将值放回

3不接收常量值,只能使用变量为其传值

4为int out参数赋值:exec[ute] :变量名:=值

举例

实现2个变量交换数据

/定义一个2变量交换数据的存储过程/

create or replace procedure exchange(a in out number,b in out number)

as

temp number;–定义一个变量

begin

temp:=a;

a:=b;

b:=temp;

end exchange;

/定义变量/

variable a NUMBER;

variable b NUMBER;

/为变量赋值/

exec :a :=1;

exec :b :=2;

/执行存储过程/

exec exchange(:a,:b);

/查看/

select :a,:b from dual;

为参数设置默认值

举例

create or replace procedure pri(a in number default 1,b in number default 1)

as

begin

dbms_output.put_line(‘a:’||a||’—-b:’||b);

end pri;

begin

pri;

pri(3);

pri(4,5);

end;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。