1. 程式人生 > >儲存過程無引數,in,out ,in out 的用法

儲存過程無引數,in,out ,in out 的用法

select * from scott.emp where empno=7839 or empno=7566 
select * from emp5
create table emp5 as select * from  scott.emp


-----------------in

create or replace procedure raisesalary(eno in number)
as
--定義一個變數儲存漲薪前的薪水
  psal emp5.sal%type;
begin
  --得到員工漲前的薪水
  select sal into psal from emp5 where empno =eno;

  --給該員工漲100
  update emp5 set sal=sal+100 where empno=eno;

  --需不需要commit?
  --注意:一般不在儲存過程或者儲存函式中,commit和rollback(一般是誰呼叫誰提交,保證事務完整性)

  --列印
  dbms_output.put_line('漲前:'||psal||'   漲後:'||(psal + 100));
end;
/

begin
 raisesalary(7839);
 raisesalary(7566); 
 commit;
end;
/


---------------in,out

create or replace procedure raisesalary1(eno in number,sal out emp5.sal%type)
as
--定義一個變數儲存漲薪前的薪水
  psal emp5.sal%type;
begin
  --得到員工漲前的薪水
  select sal into psal from emp5 where empno =eno;

  --給該員工漲100
  update emp5 set sal=sal+100 where empno=eno;

  --需不需要commit?
  --注意:一般不在儲存過程或者儲存函式中,commit和rollback(一般是誰呼叫誰提交,保證事務完整性)

  --列印
    sal:=psal;
  dbms_output.put_line('漲前:'||psal||'   漲後:'||(psal + 100));
    
    --dbms_output.put_line(sal);
end;
/

declare a emp5.sal%type;

begin
 raisesalary1(7839,a);
dbms_output.put_line('a='||a);
 raisesalary1(7566,a); 
dbms_output.put_line(a);
 commit;
 
end;

------------------in out


create or replace procedure raisesalary2(sal2 in out number) as
  --定義一個變數儲存漲薪前的薪水
  psal number;
begin
  --得到員工漲前的薪水
 -- select sal into psal from emp5 where empno = sal2;

  --給該員工漲100
  update emp5 set sal = sal + 100 where empno = sal2;
    
    

  --需不需要commit?
  --注意:一般不在儲存過程或者儲存函式中,commit和rollback(一般是誰呼叫誰提交,保證事務完整性)

  --列印
  sal2 := psal;
  dbms_output.put_line('漲前:' || psal || '   漲後:' || (psal + 100));

  --dbms_output.put_line(sal);
end;
/

declare
  a number := 7839;---得另定義一個變數去接收返回值

begin
  raisesalary2(a);
  dbms_output.put_line('a=' || a);
  a := 7566;
  raisesalary2(a);
  dbms_output.put_line(a);
  commit;

end;


select * from emp5


---------------------無引數的儲存過程

create procedure a1 as 

begin

insert into emp5(empno,ename,job) values(111,'hello','hello');
commit;
end;


begin 

 a1;
 end;


select * from emp5;----查詢執行儲存過程後是否