The difference between stored procedures, functions, and triggers in the database

Differences between stored procedures, functions, and triggers

Compare items

stored procedure

function

Is there a return value

may or may not

must have and only one

Can it be executed alone

Can

Must be executed by execute

Can the SQL statement (DML or SELECT) be called

Can not

Yes, and can be located after the FROM keyword (since a table object can be returned)

Parameter Type

Parameters in three modes of IN, OUT, IN OUT can be used

Only IN, Oracle can use IN, OUT, IN OUT three parameters

return value type

Zero or more parameter values ​​can be returned via OUT, IN OUT parameters

a single value or a table object

1) Generally speaking, the functions implemented by stored procedures are more complex, while the functions implemented by functions are more targeted. 
2) The stored procedure is generally executed as an independent part (refer to the following "Stored procedure - calling method"), and the function can be called as a part of the query statement (used after select, or after from). Since a function can return a table object, it can be placed after the FROM keyword in a query statement.

stored procedure

Stored Procedure is a set of SQL statements that are compiled and stored in the database in order to complete a specific function. Stored procedures are syntax-checked and compiled SQL statements, so the execution speed is faster than that of ordinary SQL statements. The user executes the stored procedure by specifying its name and giving parameters (if the stored procedure has parameters).

Features

1. The stored procedure is only compiled when it is created, and the stored procedure does not need to be recompiled in the future. Generally, the SQL statement is compiled once every time it is executed, so the use of the stored procedure can improve the execution speed of the database.

2. When performing complex operations on the database (for example, when performing Update, Insert, Query, and Delete on multiple tables), the complex operations can be encapsulated with stored procedures.

3. Another stored procedure can be called from within the procedure. Functions can be called in stored procedures. This can simplify a series of complex statements.

4. High security, it can be set that only a certain user has the right to use the specified stored procedure.

5. There are three kinds of parameters (IN, OUT, IN OUT), which can return multiple parameter values.

6. In ORACLE, several related procedures can be combined to form a package.

7. The stored procedure is an important object in the database, any well-designed database application should use the stored procedure.

shortcoming

1. Non-portability. The internal programming syntax of each database is different. When compatibility with multiple databases is required, it is best not to use stored procedures.

2. Business logic exists in many places. After using stored procedures, it means that some business logic in your system is not processed in the application. This architecture will increase some system maintenance and debugging costs.

basic grammar

 

createprocedure <procedure name> (<parameter list, ignored if no parameters>)

as|is

variable declaration, initialization

begin

Business processing, logic code

exception

Exception Catching, Fault Tolerance Handling

end <procedure name>;

Parameters: <parameter name> in|out|in out <parameter type, no length description>, such as: v_name varchar2

in: input parameter

out: out parameters

in out: input and output parameters

Note: as|is means as or is

 

 

call syntax

Calling syntax:

1) exec <process name>;

2) execute <process name>;

3) For the Oracle database, it can be called directly in the PL/SQL statement block. (Oracle)

function

The functions in the database include built-in functions and custom functions. The built-in functions are functions such as SUM(), COUNT(), AVG() and other built-in functions of the database. Generally, the functions we write are all custom functions.

Features

1. The function has only one parameter (IN), only one RETURN statement, and can only return a single value.

2. Functions can be called in SQL statements (DML or SELECT). Since a function can return a table object, it can be placed after the FROM keyword in a query statement.

basic grammar

 

 

create function <function name> (<parameter list, ignored if no parameters>)

return <return value type, no length specification>

as|is

variable declaration, initialization

begin

Business processing, logic code

return <return value>;

exception

Exception Catching, Fault Tolerance Handling

end <function name>;

Parameter: in input parameter

Note: Only the type of the input parameter.

 

 

call syntax

1) Call the function in a SQL statement (DML or SELECT) 
2) For Oracle database, it can be called directly in a PL/SQL statement block. (Oracle)

trigger

Trigger is a special stored procedure, the stored procedure needs to be called by the program, and the trigger will be executed automatically;

Guess you like

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