Differences between stored procedures and functions

Source: Internet
Author: User
I was asked this question by the interviewer during the interview two days ago, and I did not say anything about it. It seems that I still need to summarize the database knowledge from time to time!
I found some information on the Internet and summarized it as follows:
First, the previous image:
Differences in Oracle

In SQL Server:
Essentially, there is no difference. Only functions are as follows: only one variable can be returned. The stored procedure can return multiple objects. Functions can be embedded in SQL and called in select, but stored procedures cannot. The essence of execution is the same.
There are many function restrictions, such as the use of temporary tables, the use of only table variables, and some functions are not available, but there are relatively few restrictions on stored procedures.
1. In general, the functions implemented by stored procedures must be more complex, 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), and the function can be called as a part of the query statement (Select call), because the function can return a table object, therefore, it can be located behind the from keyword in the query statement.
4. when stored procedures and functions are executed, SQL manager will go to procedure cache to obtain the corresponding query statement. If no query statement exists in procedure cache, SQL manager compiles stored procedures and functions.
The execution plan is saved in procedure cache. After compilation, execute the execution plan in procedure cache, then, SQL Server will consider whether to save the plan in the cache based on the actual situation of each execution plan. The criterion is the possible frequency of this execution plan; the second is the cost of generating this plan, that is, the Compilation Time. The Plan saved in the cache does not need to be compiled during the next execution.

Most of the summary in SQL Server is applicable to Oracle. In the specific use process, the complexity and actual situation of the system implementation should also be checked. experienced DBAs should have been familiar with the use of the two. Please give me more advice when passing! Hoho

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.