1 , stored procedures, stored functions
Concept: The subroutines stored in the database for all user programs to call are called stored procedures and stored functions.
The same point: stored procedures and stored functions are subroutines that perform specific functions.
The difference: stored functions can return values through return , but stored procedures cannot.
2. Create and use stored procedures
Use the create procedure command to create stored procedures and stored functions.
Syntax: create (or replace) procedure procedure name (parameter list) AS pl/sql subroutine body.
For example , a hello world program
Create (or replace) procedure sayHello
As
Begin
dbms_output.put_line(‘Hello world’);
end;
3. Call the stored procedure
1、Exec sayHello();
2. Call it in another pl/sql
begin
sayHello ();
end;
4. Create a stored procedure with parameters to increase the salary of the specified employee by 100 yuan, and print the salary before and after the increase
The In keyword identifies the parameter as an input parameter
Create or replace procedure raisesalary (eno in number)
As
--Define a variable to save the salary before the increase
Psal emp.sal%type
Begin
-- Get the salary of the employee before the increase
Select sal into Psal from emp where empNo=eno;
-- Raise wages for employees
Update emp set sal=sal+100 where empNo=eno;
--Do you need to submit a commit ?
--Note : Generally, stored procedures and stored functions are no longer submitted and rolled back, and whoever calls them commits and rolls back
--print information
dbms_output.put_line(' Before rising: '||Psal||' After rising: '||(Psal+100));
End;
How to call: begin
raisesalary(1233);
raisesalary(1523);
commit();
end;
5. Syntax for creating stored functions
Create or replace function function name ( parameter list )
Return function value type
As
plSql program body
Query the annual income of an employee :
Create or replace function querysalincome(eno in number)
Return number
As
--Define variables to store employee salaries and bonuses
Psal emp.sal%type
Pcom emp.income%type
Begin
Select sal into Psal,comm into Pcom from emp where empNo=eno;
Return Psal*12+nvl(Pcom,0);
End;
6. Both stored procedures and stored functions can have out parameters, and stored procedures can return values through out parameters.
Out parameters - query an employee's name, monthly salary and position.
When to use Stored Functions / Stored Procedures?
Use a stored function if there is only one return value, otherwise use a stored procedure.
Create or replace procedure queryempinfo(eno in number,pname out varchar2,psal out number,pjob out varchar2)
As
Begin
--Get the employee's name, monthly salary and position
Select sal,empName,job into pname,psal,pjob from emp where empno=eno;
End;
Two questions:
To query all the information of an employee, if there are too many out parameters, you cannot write them one by one.
To query the information of all employees in a department , can the out parameter return a collection?
7. Accessing stored procedures and stored functions in an application is to use the CallableStatement interface
{call procedure_name( parameter list )}
For input parameters, you need to copy the output parameters and you need to declare (using the registerOutparameter method of CallableStatement ) . This method takes two parameters . The first parameter is the placeholder and the second is the type of the database corresponding to the parameter. You can call OracleTypes to indicate it.
Take out the value represented by the out parameter and use the callablestatment object . getString(?) The question mark represents the number of placeholders
8. Use the cursor in the out parameter For example: query the information of all employees in a department
包头:create or replace package myPackage AS
type empcursor is ref cursor;
procedure queryEmpList(dno in number ,emplist out empcursor);
end myPackage;
Package body: The package body needs to implement all the methods declared in the package header
Create or replace package body myPackage As
Procedure queryEmpList(dno in number,emplist out empcursor) As
Begin
Open emplist for select * from emp where deptNo=dno;
End queryEmpList;
End myPackage;
9. Access the stored procedure in the package in the application