Oracle SQL 学习

 一、创建 存储过程 的方法:

       1、错误

create or replace procedure update_sal(name varchar2(10),newsal number(10,2)) AS
begin
  update test_emp e set e.sal=newsal where lower(e.ename) = lower(name);
end update_sal;
/

        2、正确

create or replace procedure update_sal(name varchar2,newsal number) AS
begin
  update test_emp e set e.sal=newsal where lower(e.ename) = lower(name);
end update_sal;
/

 不可以把参数类型的长度也加进参数据的类型定义里头(name varchar2(10),newsal number(10,2)) )。不然会报错。

二、存储过程调用 方法:

 

--方式1
call update_sal('scott',2000);

--方式2
begin
 update_sal('scott',3000);
end;

--方式3
exec update_sal('scott',4000);

 三、函数声明

--参数和返回值的类型不可指定类型长度,不然会报错
create or replace function annaul_income(name varchar2) return number is
--定义函数的内部变量
annaul_salary number(10,2);
begin
       select sal*12 into annaul_salary from test_emp e  where lower(e.ename) = lower(name);
       return annaul_salary;
end annaul_income;
/

 四、函数调用

      调用方式1:

DECLARE CNUM NUMBER;
BEGIN
     CNUM := ppsb.annaul_income('scott');
     DBMS_OUTPUT.PUT_LINE('CNUM = ' || CNUM);
 END;



猜你喜欢

转载自conkeyn.iteye.com/blog/1431861
今日推荐