Difference between stored procedure and function in SQL

Transfer: https://www.cnblogs.com/jacketlin/p/7874009.html

Essentially there is no difference. It's just that functions are limited to returning only one variable. The stored procedure can return more than one.

The function can be embedded in sql, and can be called in select, but not stored procedures.

The execution is essentially the same. 
     There are many restrictions on functions, such as temporary tables cannot be used, only table variables can be used.

  Some functions are not available, and so on. Stored procedures have relatively few restrictions. 

    1. Generally speaking, the functions implemented by stored procedures are a little more complicated, while the functions implemented by functions are more targeted. 
      2. For stored procedures, parameters can be returned, while functions can only return values ​​or table objects. 
      3. The stored procedure is generally executed as an independent part (EXEC execution),

    The function can be called as a part of the query statement (SELECT call), because the function can return a table object,

    So it can be placed after the FROM keyword in a query statement. 
      4. When the stored procedures and functions are executed, SQL Manager will go to the procedure cache to fetch the corresponding query statements. If there is no corresponding query statement in the procedure cache, SQL Manager will compile the stored procedures and functions. 
      The execution plan is saved in the procedure cache. After the compilation is completed, the execution plan in the procedure cache is executed. After that, SQL SERVER will consider whether to save the plan in the cache according to the actual situation of each execution plan. Judge One of the criteria is the frequency with which this execution plan may be used; the second is the cost of generating this plan, that is, the time-consuming compilation. The plan saved in the cache does not need to be compiled the next time it is executed.

Guess you like

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