Stored Procedures, Stored Functions in Oracle

1. Stored procedure

        A stored procedure is actually a set of compiled SQL statements, and the difference between a stored function and a stored procedure is that the function can return , and another rule is: if the query has only one return value or data, the stored function is preferred .

advantage:       

1. Encapsulate some highly repetitive operations into a stored procedure, simplifying the calls to these SQLs

2. Batch processing: SQL+loop, reducing traffic, that is, "running batches"

3. Unified interface to ensure data security

Compared with oracle database, MySQL's stored procedures are relatively weak and used less.


Create stored procedure syntax:

        create [or replace] procedure procedure name (parameter list) is PLSQL subroutine body;

taps : is can also be replaced with as

Take the emp table in Oracle as an example: create a stored procedure named TestOne whose input parameter is epno, and the output parameter is psal, phiredate.

create or replace procedure TestOne(epno in number,
                                                            psal out number,
                                                   phiredate out date) as
begin
  select sal,emp.hiredate
into psal,phiredate from emp where emp.empno=epno;

end ;

call in sqldeveloper

set serveoutput on

exec TestOne(7839);

call raw JDBC form in java code

    String sql = "{call TestOne(?,?,?)}"; //Call stored procedure syntax {call <procedure-name>[(<arg1>,<arg2>, ...)]}
Connection con = this .getConnection(); // Self-encapsulated JDBC connection
CallableStatement call=null; // CallableStatement is the interface used to operate stored procedures
call = con.prepareCall(sql);
//Assign the in parameter
call.setInt(1, 7839);
//Declare the out parameter. There are two ways to get the type OracleTypes and java.sql.types
call.registerOutParameter(2, OracleTypes.INTEGER);
call.registerOutParameter(3,java.sql.Types.DATE);
call .execute();
int sal = call.getInt(2);
Date date = call.getDate(3);
System.out.println(sal+"|"+date);

2. Stored functions

A function is a named stored procedure that can take parameters and return a computed value. Functions and procedures are similar in structure, but) 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.

Create stored function syntax:

        create [or replace] function function name (parameter list) 

        return function value type            

        is PLSQL subroutine body;

Example: Enter an employee number, calculate 12 months salary including bonus, and return

create or replace function queryEmp(peno in number)
return number
as
psal emp.sal%type;
pcomm emp.comm%type;
begin
  select sal,comm into psal,pcomm from emp where empno=peno;
  return psal*12+nvl(pcomm,0);
  end;

Call the raw JDBC form in java code:

String sql ="{?= call queryemp(?)}"; //Call function syntax  {?= call <procedure-name>[(<arg1>,<arg2>, ...)]}
Connection con = this. getConnection();
CallableStatement call=null;
call = con.prepareCall(sql);
//For the first one? Make a declaration
call.registerOutParameter(1, OracleTypes.INTEGER);
call.setInt(2, 7839);
call.execute();
//Get the returned declaration
int sal = call.getInt(1);
System.out.println( sal);



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326125684&siteId=291194637