Differences and relations stored procedures and functions

First, the function

  Function is stored in a similar procedure, it is stored in the database named PL-SQL block. The main characteristic function is that it must have a return value. Return by the return type of the function is specified. Can be returned from the function by the return expression statement anywhere in the function, the return type must be declared and return the same type.

Second, the function and advantages of stored procedures:

  1, the code used in common may need only be written once, needed any application of the code call (.net, c ++, java, can make DLL library).

  2, several write this, several maintenance updates, shared approach simplifies the development and maintenance of applications, improving efficiency and performance.

  3, this modular approach such a complex problem, large program and gradually reduced to a few simple, small program portion, respectively, for the preparation, the structure of the program is more clear, simple, and easy to implement.

  4, data processing may be provided between the respective developers, the control flow consistency, like message.

  5, save memory space. They are in a compressed form is stored in the external memory, only when a call is being placed into memory for processing. And when multiple users call the same stored procedure or function, only need to be loaded once.

  6, improve the safety and integrity of the data. By some operations side of the data to a stored procedure or function, you can have permission to execute the statement by whether to grant the user, to restrict certain users of the database for these operations.

Third, the difference between functions and stored procedures:

  1, stored procedures user database accomplish a specific task or tasks (e.g., insert, delete, etc.), functions to return specific data .

  2, stored procedure declaration with the procedure, function by function.

  3, the stored procedure does not require the return type , the function must return type .

  4, a stored procedure can be performed independent PLSQL , a function not as an independent execution stand-PLSQL , must be part of the expression.

  5, stored procedures only through the out and in / out to the return value , in addition to the function than can be used out, in / out, return the return value can also be used.

  6, sql statement (DML or SELECT) stored procedure call is not available, and can function.

Fourth, the application of occasions:

  1, if the need to return multiple values and non-return value, the use of stored procedures ; If you only want to return a value, use the function .

  2, the stored procedure is generally used to perform a specified action , the function is generally used to calculate and return a value.

  3, it can then call the SQL internal functions to perform complex computational problems, but you can not call a stored procedure.

Fifth, differences and connections stored procedures and stored functions

  The same point: 1. Create a similar grammatical structure , can carry multiple incoming and outgoing parameters parameters.

          2 is a compilation, multiple times.

  Different points: a stored procedure defined by the keyword procedure defined function with the function .

      2. The stored procedure return value can not be returned , but may function, and the function must have the return clause .

      3. Run a slightly different way, the stored procedure performed in two ways (1. execute2. End and begin to use), two kinds of functions in addition to the stored procedure may also be used as an expression, for example, placed in the select (select f1 () form dual;).

Summary: If only one return value, with memory function, otherwise, the general use of stored procedures.

Guess you like

Origin www.cnblogs.com/karrya/p/11600130.html