oracle學習 建立函式呼叫函式及儲存過程

2021-09-25 11:02:45 字數 3314 閱讀 1573

create or replace function f_get_name(t_empno in varchar2)

return varchar2 is

v_emp_name emp.ename%type;

begin

select e.ename into v_emp_name

from emp e where e.empno=t_empno;

return v_emp_name;

end f_get_name;

select f_get_name('7698') from dual

--根據部門號查詢部門名稱,使用函式 查詢人員姓名及部門名稱

create or replace function f_get_name_by_deptno(t_deptno in varchar2)

return varchar2 is

v_dept_name dept.dname%type;

begin

select d.dname into v_dept_name

from dept d where d.deptno=v_dept_name;

return v_dept_name;

end f_get_name_by_deptno;

select ename,f_get_name_by_deptno(deptno) from emp

select * from emp

--create or replace procedure 

--儲存過程的呼叫

--exec 過程名()

create or replace procedure proc_demo(

dept_no number default 10,sal_sum out number,emp_count out number)

isbegin

select sum(sal),count(*) into sal_sum,emp_count

from emp where deptno=dept_no;

end proc_demo;

create or replace procedure proc_demo2(up_count out number) is

begin

up_count := 0;

declare

cursor e_job is

select empno, job from emp;

begin

for v_job in e_job loop

if v_job.job = 'clerk' then

update emp set job = '辦事員' where empno = v_job.empno;

elsif v_job.job = 'salesman' then

update emp set job = '銷售人員' where empno = v_job.empno;

elsif v_job.job = 'manager' then

update emp set job = '經理' where empno = v_job.empno;

else

update emp set job = '其他' where empno = v_job.empno;

end if;

up_count := up_count + 1;

end loop;

commit;

end;

end proc_demo2;

select * from emp

–過程名

create table manager as select * from emp

select * from manager

--向manager表中插入職位為經理年薪大於7w的資料

--如果manager表裡已經存在此員工,則不插入

--如果不存在,在插入。輸出引數是插入的記錄數

create or replace procedure proc_demo3(

up_no out number)

is begin

up_no:=0;

declare

cursor c_year_sal is

select empno, nvl(e.sal,0)*12+nvl(e.comm,0) as yearsal from emp;

begin

if c_year_sal.yearsal>70000 then

insert into manager select * from emp where empno = c_year_sal.empno;

else

exit;

end if;

commit;

end;

end proc_demo3;

create or replace procedure proc_demo(

dept_no number default 10,

sal_sum out number,

emp_count out number)

isbegin

select sum(sal),count(*) into sal_sum,emp_count

from emp where deptno=dept_no;

end proc_demo;

create or replace procedure add_man2(m_num out number) is

u_row_num number;

begin

m_num := 0;

select count(1)

into u_row_num

from manager

where job = '經理'

and (nvl(sal, 0) * 12 + nvl(comm, 0)) > 70000;

if (u_row_num < 1) then

--不存在

insert into manager

(empno, ename, job, sal, comm)

values

(8654, '小陳', '經理', 6000, 500);

commit;

m_num := m_num + 1;

end if;

end;

select * from manager;

oracle函式的建立和呼叫

函式的建立create or replace function addfunction x in number,y out number return number is results number 0 begin results x 1 dbms output.put line results ...

mysql 建立函式 mysql裡怎樣建立函式

mysql裡建立函式的方法 首先檢視建立函式的功能是否開啟 然後如果value處值為off,則需將其開啟 接著建立函式時,先選擇資料庫 最後進行測試即可。mysql裡建立函式的方法 一 檢視建立函式的功能是否開啟 mysql show variables like func variable nam...

Oracle 建立函式

create orreplace function sf score pm p in stuid in varchar2,學號 p in courseid in varchar2 課程id return number is ls pm number 0 ls score number 0 begin...