To create a function:
format:Create or Replace function func ( parameter parameter type )
Return number
Is
Begin
-------- business logic ---------
End;
--Create a functionCreate or Replace functionFunc (DNO Number)return Number isT_max Number;begin Select Max(SAL) intoT_max fromEMP TwhereDeptno=DNO; returnT_max;End ;--examples of calling stored procedures and calling functions (differences)DeclareT_maxsal Number;begint_maxsal:=FuncTen); Dbms_output.put_line (t_maxsal); Dbms_output.put_line (func (Ten));End;Create or Replace procedure proc(DNOinch Number, maxsal out Number) isT_maxsal Number;begin Select Max(SAL) intoT_maxsal fromEmpwhereDeptno=DNO; Maxsal:=t_maxsal;End;DeclareT_sal Number:= 0;begin proc(Ten, t_sal); Dbms_output.put_line (t_sal);End;Create or Replace functionFunc (A NumberA Number)return Number isT_result Number:= 0;beginT_result:=A+b; returnT_result;End;beginDbms_output.put_line (func (Ten, -));End;
--delete functionDrop functionfunc;
--Delete stored proceduresDrop procedure proc;--Create a package (you can write stored procedures and functions in the package)Create or ReplacePackage PackprocedureProadd (A NumberB Number)procedureProsub (A NumberB Number)Endpack;beginPack.proadd (3,1); Pack.prosub (3,2);End;
Note: The above call refers to the SQL window called in the Plsql
Examples of Oracle creating functions and calling stored procedures and calling functions (differences)