Stored procedure overview _ MySQL

Source: Internet
Author: User
Stored procedure overview in large database systems, stored procedures and triggers play an important role. Both stored procedures and triggers are a collection of SQL statements and flow control statements. Just


In essence, triggers are also a storage process. The storage process generates execution methods during computation. Therefore, it will be executed quickly when it is re-running later. SQL Server 2000 not only provides a user-defined stored procedure function, but also provides many system stored procedures that can be used as tools.

12.1.1 concept of stored procedure
Stored Procedure is a set of SQL statements for specific functions. it is compiled and Stored in the database. You can run the stored procedure by specifying the name of the stored procedure and providing parameters (if the stored procedure has parameters.

In SQL Server versions, stored procedures are classified into two types: Stored procedures provided by the system and custom stored procedures. System procedures are mainly stored in the master database and prefixed with sp _. The system stored procedures mainly obtain information from the system table to provide support for the system administrator to manage SQL Server. Through System stored procedures, many administrative or information activities (such as understanding database objects, database information) in the ms SQL Server can be successfully and effectively completed. Although these system stored procedures are stored in the master database, they can still be called in other databases. you do not need to add the database name before the stored procedure name during the call. When a new database is created, some system stored procedures are automatically created in the new database. A user-defined stored procedure is a stored procedure that is created by the user and can complete a specific function (for example, querying the data information required by the user. The stored procedures involved in this chapter mainly refer to user-defined stored procedures.

12.1.2 advantages of stored procedures
When ms SQL Server is used to create an application, Transaction-SQL is a major programming language. If you use Transaction-SQL for programming, there are two methods. First, store the Transaction-SQL program locally and create an application to send commands to SQL Server to process the results. Second, some programs written using Transaction-SQL can be stored in SQL Server as stored procedures, and applications can be created to call stored procedures, the stored procedure of processing data results can return the result set to the caller by receiving parameters. the format of the result set is determined by the caller. the status value is returned to the caller, indicating whether the call is successful or failed; includes database operation statements, and can call another stored procedure in one stored procedure.

We usually prefer the second method, that is, using stored procedures in SQL Server rather than calling a program written in Transaction-SQL on the client's computer, because the stored procedures have the following advantages:

(1) stored procedures allow standard component programming
After a stored procedure is created, it can be called multiple times in the program without re-writing the SQL statement of the stored procedure. Database Professionals can modify stored procedures at any time, but it has no impact on the application source code (because the application source code only contains the calling statements of stored procedures ), this greatly improves the portability of the program.

(2) fast execution of stored procedures
If an operation contains a large number of Transaction-SQL code or is executed multiple times, the stored procedure is much faster than the batch processing. Because the stored procedure is pre-compiled, when a stored procedure is run for the first time, the query optimizer analyzes and optimizes the stored procedure and provides the execution plan that is finally stored in the system table. The batch processing Transaction-SQL statement must be compiled and optimized each time it is run, so the speed is relatively slow.

(3) stored procedures can reduce network traffic
If the Transaction-SQL statement involved in the same operation on data database objects (such as query and modification) is organized into a stored procedure, when the stored procedure is called on the client's computer, only the calling statement is sent on the network. otherwise, multiple SQL statements are sent, greatly increasing network traffic, reduce network load.

(4) stored procedures can be fully utilized as a security mechanism
The system administrator restricts the permissions to execute a stored procedure to avoid unauthorized users accessing data and ensure data security. (We will provide a clearer introduction to stored procedures in Chapter 14 "SQL Server user and security management)

Note: Although stored procedures have both parameters and return values, they are different from functions. The returned value of a stored procedure only indicates whether the execution is successful and cannot be directly called as a function, that is, when a stored procedure is called, the stored procedure name must contain the EXEC reserved word (for how to execute the stored procedure, see the next word in this chapter ).

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.