Stored Procedures and Stored Functions
Refers to the
subprograms
called stored procedures and stored functions.
What is the difference between stored procedure and stored function?
Stored function: The function value can be returned through the return statement.
Stored procedure: no
Other than that we can consider them to be exactly the same.
stored procedure
Create stored procedure
Use the create procedure command to resume stored procedures.
grammar:
create [or replace] procedure procedure name (parameter list)
as
PLSQL subroutine body;
print hello word
--print hello world create or replace procedure sayhelloworld as --Description section begin dbms_output.put_line('hello world'); end; /After compilation:
Call the stored procedure method:
1. exec process name
2、begin
procedure name;
procedure name;
end;
/
Test calling stored procedure
--Connect to the database
C:\WINDOWS\system32>sqlplus scott/
[email protected]
:1521/orcl
SQL>--call method one
SQL> set serveroutput on
SQL> exec sayhelloworld;
hello world
PL/SQL procedure completed successfully.
SQL> -- call method two:
SQL> begin
2 sayhelloworld();
3 sayhelloworld();
4 end;
5 /
hello world
hello world
PL/SQL procedure completed successfully.
|
--Increase the salary of designated employees by 100, and print the salary before and after the increase create or replace procedure raiseSalary(eno in number) --in is the input parameter as --Description section psal emp.sal%type; begin -- Get the salary before the increase select sal into psal from emp where empno=eno; update emp set sal=sal+100 where empno=eno; --Do you want to commit? --In order to ensure that in the same transaction, whoever calls commit commits dbms_output.put_line('Before rising: '||psal||' After rising: '||(psal+100)); end; /test:
stored function
A function is a named stored procedure that takes parameters and returns a computed value. Functions and procedures are similar in structure, but must have a return clause that returns the function value. The function specification specifies the function name, the type of the result value, and the parameter type.
Stored function syntax:
create[or replace] function function name (parameter list)
return function value type
as
PLSQL subroutine body;
Query employee's annual income
-- Query the annual income of an employee create or replace function queryempincome(eno in number) return number as --Monthly salary and bonus psal emp.sal%type; pcomm emp.comm%type; begin select sal,comm into psal,pcomm from emp where empno=eno; -- return annual income return psal*12+nvl(pcomm,0); end; /test
in and out in procedures and functions
In general, the difference between a procedure and a function is that a function can have a return value; a procedure has no return value.
But both procedures and functions can specify one or more output parameters through out, and we can use out parameters to return multiple values in procedures and functions.
When to use stored procedure/stored function?
Principle
(not required):
If there is only one return value, use a stored function; otherwise, use a stored procedure.
stored procedure
create or replace procedure queryEmpInfo(eno in number, pname out varchar2, psal out number, pjob out varchar2) as begin select ename,sal,empjob into pname,psal,pjob from emp where empno=eno; end;