创建一个存储过程的语法:
create [or replace] procedure 过程名
[(参数名 [IN | OUT | IN OUT] 数据类型 ...]
{IS | AS}
{说明部分}
begin
语句序列
[exception 例外处理]
end [过程名]
-
CREATE TABLE Student
-
(Sno CHAR(9) PRIMARY KEY,
-
Sname CHAR(20) NOT NULL,
-
Ssex CHAR(4),
-
Sage SMALLINT,
-
Sdept CHAR(20)) tablespace gaospace;
-
-
例子:
创建一个添加学生的存储过程。
-
create or replace procedure addnewstudent(
-
p_sno student.sno%type,
-
p_sname student.sname%type,
-
p_ssex student.ssex%type,
-
p_sage student.sage%type,
-
p_sdept student.sdept%type)
-
As
-
begin
-
insert into student values(p_sno, p_sname, p_ssex, p_sage, p_sdept);
-
commit;
-
end addnewstudent;
调用:
-
begin
-
addnewstudent('0209','赵利','男',21,'cs');
-
end;
参数类型:
这里肯定是传入参数 IN:
-
create or replace procedure addnewstudent2(
-
p_sno IN student.sno%type,
-
p_sname IN student.sname%type,
-
p_ssex IN student.ssex%type,
-
p_sage IN student.sage%type,
-
p_sdept IN student.sdept%type)
-
As
-
begin
-
insert into student values(p_sno, p_sname, p_ssex, p_sage, p_sdept);
-
commit;
-
end addnewstudent2;
-
-
-
begin
-
addnewstudent2('0210','赵五','男',21,'cs');
-
end;
如果换成传出参数OUT,会怎么样了?
-
create or replace procedure addnewstudent3(
-
p_sno OUT student.sno%type,
-
p_sname OUT student.sname%type,
-
p_ssex OUT student.ssex%type,
-
p_sage OUT student.sage%type,
-
p_sdept OUT student.sdept%type)
-
As
-
begin
-
insert into student values(p_sno, p_sname, p_ssex, p_sage, p_sdept);
-
commit;
-
end addnewstudent3;
-
-
-
begin
-
addnewstudent3('0211','高五','男',21,'cs');
-
end;
返回error:
ORA-06550: 第 2 行, 第 17 列:
PLS-00363: 表达式 '0211' 不能用作赋值目标
ORA-06550: 第 2 行, 第 24 列:
PLS-00363: 表达式 '高五' 不能用作赋值目标
ORA-06550: 第 2 行, 第 33 列:
PLS-00363: 表达式 '男' 不能用作赋值目标
ORA-06550: 第 2 行, 第 39 列:
PLS-00363: 表达式 '21' 不能用作赋值目标
ORA-06550: 第 2 行, 第 42 列:
PLS-00363: 表达式 'cs' 不能用作赋值目标
ORA-06550: 第 2 行, 第 2 列:
PL/SQL: Statement ignored
如果换成传出参数IN OUT,会怎么样了?
-
create or replace procedure addnewstudent5(
-
p_sno in out student.sno%type,
-
p_sname in out student.sname%type,
-
p_ssex in out student.ssex%type,
-
p_sage in out student.sage%type,
-
p_sdept in out student.sdept%type)
-
As
-
begin
-
insert into student values(p_sno, p_sname, p_ssex, p_sage, p_sdept);
-
commit;
-
end addnewstudent5;
-
-
-
begin
-
addnewstudent5(\'0211\',\'高五\',\'男\',21,\'cs\');
-
end;
报错误:
第 2 行出现错误:
ORA-06550: 第 2 行, 第 17 列:
PLS-00363: 表达式 '0211' 不能用作赋值目标
ORA-06550: 第 2 行, 第 24 列:
PLS-00363: 表达式 '高五' 不能用作赋值目标
ORA-06550: 第 2 行, 第 33 列:
PLS-00363: 表达式 '男' 不能用作赋值目标
ORA-06550: 第 2 行, 第 39 列:
PLS-00363: 表达式 '21' 不能用作赋值目标
ORA-06550: 第 2 行, 第 42 列:
PLS-00363: 表达式 'cs' 不能用作赋值目标
ORA-06550: 第 2 行, 第 2 列:
PL/SQL: Statement ignored
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26435490/viewspace-1077840/
,如需转载,请注明出处,否则将追究法律责任。