PLSQL learning stored procedures and stored functions

Stored procedures and stored functions

In Oracle, the concepts of stored procedures and stored functions are actually similar. Generally, we can mix them. It's just that sometimes the use process is better in some cases, and the function is better in some cases. The following will explain when to use the process or the function.

First of all, before we learn stored procedures and stored functions, we must first understand why we should learn them ...

In fact, stored procedures and functions are concepts similar to our functions in Java ...

So far, our PLSQL has several shortcomings:

  • PLSQL can't encapsulate it, and the entire piece of code must be copied to call each time it is called
  • Sometimes, when we want to save the PLSQL code, we can only manually save it on the hard disk, which is very troublesome.
  • We learn the database for the program to be able to call, but PLSQL can not be called by the program (java)

Therefore, stored procedures and stored functions can solve the above problems, can encapsulate the code, save it in the database, and let the programming language call...

Syntax of stored procedures and functions

The syntax of the procedure:


create [or replace] procedure 过程名[(参数列表)]  
as
        PLSQL程序体;【begin…end;/】

The syntax of the function:


CREATE [OR REPLACE] FUNCTION 函数名【(参数列表) 】
 RETURN  返回值类型
AS
PLSQL子程序体;

【begin…end;/】

Whether it is a procedure or a function, the as keyword replaces the declare keyword.


Create the first process:


CREATE OR REPLACE PROCEDURE hello
AS
  BEGIN
    dbms_output.put_line('hello world');
  END;

Three ways to call the procedure:

  • exec procedure name [used in SQLPLUS]
  • PLSQL program call
  • Java call

PLSQL call


BEGIN
  hello();

END;

Create a parameterized stored procedure raiseSalary (number) to increase the salary of employee No. 7369 by 10%, and demonstrate the usage of in. The default is in, which is not case sensitive.

CREATE or REPLACE PROCEDURE bb(pempno in NUMBER)
  AS
  BEGIN
    UPDATE EMP
    SET sal = sal * 1.2
    WHERE empno = pempno;

  END;

transfer:


  BEGIN
    bb(7369);
  END;

Create a parameterized stored procedure findEmpNameAndSalAndJob (number), query the name, position, and monthly salary of employee No. 7788, return multiple values, and demonstrate the usage of out

Creation process: The default value of the parameter in the process is IN. If it is output, then we have to specify it as OUT.


CREATE OR REPLACE PROCEDURE find(pempno IN NUMBER, psal OUT VARCHAR2, pename OUT VARCHAR2, pjob OUT VARCHAR2)
AS
  BEGIN
    SELECT
      ename,
      sal,
      job
    INTO pename, psal, pjob
    FROM emp
    WHERE empno = pempno;
  END;

Call: When calling, the psal, pname, and pjob used are not defined when calling, so we need to define the variables before using!


DECLARE

  psal   emp.sal%TYPE;
  pename emp.ename%TYPE;
  pjob   emp.job%TYPE;

BEGIN
  find(7369, psal, pename, pjob);

  dbms_output.put_line(psal || pename || pjob);

END;/

Create a parameter storage function findEmpIncome (number), query the annual income of employee No. 7369, demonstrate the usage of in, the default is in


CREATE OR REPLACE FUNCTION findEmpIncome(pempno IN NUMBER)
  --这里指定的是返回值类型
  RETURN NUMBER
AS
  income NUMBER;
  BEGIN
    SELECT sal * 12
    INTO income
    FROM emp
    WHERE empno = pempno;

    /*在PLSQL中一定要有return语句*/
    RETURN income;
  END;

Call: In PLSQL, the assignment statement is not directly "=", but: =


DECLARE
  income number;
BEGIN
  income := findEmpIncome(7369);
  dbms_output.put_line(income);

END;/

If you write =, then the following error will occur:


[2017-07-11 13:58:14] [65000][6550] ORA-06550: 第 4 行, 第 10 列: 
PLS-00103: 出现符号 "="在需要下列之一时:
 := . ( @ % ;
ORA-06550: 第 4 行, 第 31 列: 
PLS-00103: 出现符号 ";"在需要下列之一时:
 . ( ) , * % & -
   + / at mod remainder rem <an exponent (**)> and or ||
   multiset
ORA-06550: 第 7 行, 第 4 列: 
PLS-00103: 出现符号 "end-of-file"在需要下列之一时:
 end
   not pragma final instantiable order overriding static member
   constructor map

Create a parameter storage function findEmpNameAndJobAndSal (number), query the name (return), position (out), monthly salary (out) of employee No. 7788, and return multiple values


CREATE OR REPLACE FUNCTION findEmpNameAndJobAndSal(pempno IN NUMBER, pjob OUT VARCHAR2, income OUT NUMBER)
  --这里指定的是返回值类型
  RETURN VARCHAR
AS
  /*查询出来的字段与列名相同,就使用列名相同的类型就行了。*/
  pename emp.ename%TYPE;
  BEGIN
    SELECT
      sal,
      ename,
      job
    INTO income, pename, pjob
    FROM emp
    WHERE empno = pempno;

    /*在PLSQL中一定要有return语句*/
    RETURN pename;
  END;

Call functions:


DECLARE

  /*输出的字段与列名的类型是相同的。*/
  income emp.sal%TYPE;
  pjob   emp.job%TYPE;
  pename emp.ename%TYPE;
BEGIN
  pename := findEmpNameAndJobAndSal(7369, pjob, income);
  dbms_output.put_line(pename || pjob || income);

END;/

Use scenarios of procedures and functions

We found that the difference between a process and a function is actually not that big. Generally, when we can use a function to achieve it, we can also use a process to achieve it....

However, there are always situations where using a function is better than using a process, and using a process is better than using a function. When should you use a process and when should you use a function? ? ?

It is not difficult to find that the function must have a return value. When we are calling, we can just get the return value directly when we accept it.

In other words

  • When the return value has only one parameter, then use the stored function!
  • When the return value has no parameters or more than one parameter, then use the procedure!


Author: Java3y
link: http: //www.imooc.com/article/25261
Source: Mu class network
article published in the original Mu-class network, please indicate the source, thank you

Guess you like

Origin blog.csdn.net/qq_39331713/article/details/93600947