oracle 基础温习之 存储过程

创建一个存储过程的语法:
create [or replace] procedure 过程名
            [(参数名 [IN | OUT | IN OUT] 数据类型  ...]

{IS  | AS}
        {说明部分}
begin
         语句序列
         [exception 例外处理]
end [过程名]



  1. CREATE TABLE Student
  2.        (Sno CHAR(9) PRIMARY KEY,
  3.         Sname CHAR(20) NOT NULL,
  4.         Ssex CHAR(4),
  5.         Sage SMALLINT,
  6.         Sdept CHAR(20)) tablespace gaospace;






例子:
创建一个添加学生的存储过程。

  1. create or replace procedure addnewstudent(
  2. p_sno student.sno%type,
  3. p_sname student.sname%type,
  4. p_ssex student.ssex%type,
  5. p_sage student.sage%type,
  6. p_sdept student.sdept%type)
  7. As
  8. begin
  9. insert into student values(p_sno, p_sname, p_ssex, p_sage, p_sdept);
  10. commit;
  11. end addnewstudent;



调用:

  1. begin
  2.  addnewstudent('0209','赵利','男',21,'cs');
  3. 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,会怎么样了?

  1. create or replace procedure addnewstudent3(
  2. p_sno OUT student.sno%type,
  3. p_sname OUT student.sname%type,
  4. p_ssex OUT student.ssex%type,
  5. p_sage OUT student.sage%type,
  6. p_sdept OUT student.sdept%type)
  7. As
  8. begin
  9. insert into student values(p_sno, p_sname, p_ssex, p_sage, p_sdept);
  10. commit;
  11. end addnewstudent3;


  12. begin
  13.  addnewstudent3('0211','高五','男',21,'cs');
  14. 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,会怎么样了?

  1. create or replace procedure addnewstudent5(
  2. p_sno in out student.sno%type,
  3. p_sname in out student.sname%type,
  4. p_ssex in out student.ssex%type,
  5. p_sage in out student.sage%type,
  6. p_sdept in out student.sdept%type)
  7. As
  8. begin
  9. insert into student values(p_sno, p_sname, p_ssex, p_sage, p_sdept);
  10. commit;
  11. end addnewstudent5;


  12. begin
  13.  addnewstudent5(\'0211\',\'高五\',\'男\',21,\'cs\');
  14. 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博客 ” ,链接:http://blog.itpub.net/26435490/viewspace-1077840/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26435490/viewspace-1077840/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值