The difference and connection between function and stored procedure in oracle

In oracle, functions and stored procedures are often used, and their syntax has many similarities, but also has its own characteristics. Just finished learning functions and stored procedures, let me share with you the difference between functions and stored procedures that I have summarized.

First, the stored procedure

1. Definition

  A stored procedure is a subroutine that is stored in the database and provides all user program calls. The keyword that defines the stored procedure is procedure.

2. Create a stored procedure

  create [or replace] procedure stored procedure name

  [(parameter 1 type, parameter 2 out type...)]                                       

  as

    variable name type;

  begin

    body of program code

  end;

  Example 1: No parameter, no return

create or replace procedure p1
--or replace means that when the stored procedure is created, if the storage name exists, the original stored procedure will be replaced and re-created
-- When there is no parameter list, no need to write ()
as
begin
  dbms_output.put_line('hello world');
end;

--Execute stored procedure mode 1
set serveroutput on;
begin
  p1();
end;
--Execute stored procedure mode 2
set serveroutput on;
execute p1();

 Example 2: Participate and return

create or replace procedure p2
(name in varchar2,age int,msg out varchar2)
--In the parameter list, when declaring the variable type, remember not to write uppercase or lowercase, just write the type name, such as the declaration of the name variable in the parameter list
--In the parameter list, the input parameters are represented by in, and the output parameters are represented by out. When not written, the default is the input parameter.
------------The input parameter cannot carry the value out, the output parameter cannot carry the value in, when you want to carry the value in, but also want to carry the value out, you can use in out
as
begin
  msg:='name'||name||', age'||age;
  --In addition to using := when assigning, you can also use into to achieve
  --The above clause is equivalent to select 'name'||name||', age'||age into msg from dual;
end;
--Execute the stored procedure
set serveroutput on;
declare
  msg varchar2(100);
begin
  p2('Zhang San',23,msg);
  dbms_output.put_line(msg);
end;

 Example 3: There are in out parameters in the parameter list

create or replace procedure p3
(msg in out varchar2)
--When you want to carry the value in and you want to carry the value out, you can use in out
as
begin
  dbms_output.put_line(msg); --The output is the value carried in
  msg:='I am the value carried from the stored procedure';
end;


--Execute the stored procedure
set serveroutput on;
declare
  msg varchar2(100):='I am the value that I carry in';
begin
  p3(msg);
  dbms_output.put_line(msg);
end;

 Example 4: Defining parameters in a stored procedure

create or replace procedure p4
as
  -- list of parameters defined in the stored procedure
  name varchar(50);
begin
  name := 'hello world';
  dbms_output.put_line(name);
end;
--- execute the stored procedure
set serveroutput on;
execute p4();

 

 Summary: 1. The keyword to create a stored procedure is procedure.

      2. The parameters in the parameter list can be modified with in, out, in out, and the parameter type must not be capitalized. There can be multiple in-out parameters in the list.

      3. The parameter list defined in the stored procedure does not need to be declared with declare. When declaring the parameter type, you must bring the size.

    4.as can be replaced with is.

      5. Calling a procedure with output parameters must declare variables to receive the output parameter values.

      6. There are two ways to execute a stored procedure, one is to use execute, and the other is to wrap it with begin and end.

      Although the stored procedure has many advantages, it cannot use the return value. When we need to use return to return a value, we can use functions.

 

Second, the storage function

1. The structure of a function is similar to that of a stored procedure, but the function must have a return clause for returning the function value.

create or replace function f1
return varchar--must have a return value, and no need to increase the size when declaring the return value type
as
  msg varchar(50);
begin
   msg := 'hello world';
   return msg;
end;

--Execute function mode 1
select f1() from dual;
--Execute function mode 2
set serveroutput on;
begin
  dbms_output.put_line(f1());
end;

 

 Third, the difference and connection between stored procedure and stored function

  Similarities: 1. The creation syntax is similar, and both can carry multiple incoming and outgoing parameters.

          2. All are compiled once and executed multiple times.

  Differences: 1. The stored procedure definition keyword uses procedure, and the function definition uses function.

      2. The return value cannot be used in the stored procedure, but it can be used in the function, and there must be a return clause in the function.

      3. The execution methods are slightly different. There are two execution methods of stored procedures (1. Use execute2. Use begin and end). In addition to the two methods of stored procedures, functions can also be used as expressions, such as in select (select f1() form dual;).

Summary: If there is only one return value, use a stored function, otherwise, generally use a stored procedure.

Guess you like

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