SQL Server Basics: Stored Procedures
Simply put, a stored procedure is a collection of one or more SQL statements, which can be regarded as a batch file, but its role is not limited to batch processing. This article mainly introduces the use of variables, the creation, calling, viewing, modifying and deleting operations of stored procedures and stored functions.
1: Overview of stored procedures
The stored procedures in SQL Server are code segments written using T_SQL. Its purpose is to easily query information from system tables, or to complete management tasks related to updating database tables and other system management tasks. The T_SQL statement is the programming interface between the SQL Server database and the application program. In many cases, some codes will be written repeatedly by developers. If the code with the same function is written every time, it will not only be cumbersome and error-prone, but also reduce the operating efficiency of the system because SQL Server executes statements one by one.
In short, a stored procedure means that in order to achieve a specific task, SQL Server writes some fixed operation statements that need to be called multiple times into program segments. These program segments are stored on the server, and the database server calls them through the program.
Advantages of stored procedures:
Stored procedures speed up system operation. Stored procedures are only compiled when they are created and do not need to be recompiled every time they are executed in the future.
Stored procedures can encapsulate complex database operations and simplify the operation process, such as updating, deleting multiple tables, etc.
It can realize modular programming, and the stored procedure can be called multiple times, providing a unified database access interface and improving the maintainability of the application.
Stored procedures can increase the security of the code. For users who cannot directly operate the objects referenced in the stored procedures, SQL Server can set the user's execution permissions for the specified stored procedures.
Stored procedures can reduce network traffic. The stored procedure code is stored directly in the database. During the communication process between the client and the server, a large amount of T_SQL code traffic will not be generated.
Disadvantages of stored procedures:
Database transplantation is inconvenient. Stored procedures depend on the database management system. The operation code encapsulated in SQL Server stored procedures cannot be directly transplanted to other database management systems.
Does not support object-oriented design, cannot encapsulate logical business in an object-oriented way, or even form a general business logic framework that can support services.
The code is poorly readable and difficult to maintain. Clustering is not supported.
2: Classification of stored procedures
1. System stored procedures
System stored procedures are stored procedures provided by the SQL Server system itself, which can be used as commands to perform various operations.
System stored procedures are mainly used to obtain information from system tables. Use system stored procedures to complete the management of the database server, provide help to system administrators, and provide convenience for users to view database objects. The system stored procedures are located in the database server, and Starting with sp_, system stored procedures are defined in system-defined and user-defined databases, and there is no need to add a database qualified name before the stored procedure when calling. For example: the sp_rename system stored procedure can modify the name of a user-created object in the current database, the sp_helptext stored procedure can display text information of rules, default values or views, and many management tasks in the SQL SERVER server are completed by executing system stored procedures. , much system information can also be obtained by executing system stored procedures.
System stored procedures are created and stored in the system database master. Some system stored procedures can only be used by system administrators, while some system stored procedures can be used by other users through authorization.
2. User stored procedures (custom stored procedures)
Custom stored procedures are a collection of T_SQL statements written by users using T_SQL statements in order to achieve a specific business requirement in the user database. Custom stored procedures can Accept input parameters, return results and information to the client, return output parameters, etc. When creating a custom stored procedure, adding "##" in front of the stored procedure name indicates that a global temporary stored procedure is created; adding "#" in front of the stored procedure indicates that a local temporary stored procedure is created. A local temporary stored procedure can only be used within the session in which it was created and will be deleted when the session ends. Both stored procedures are stored in the tempdb database.
User-defined stored procedures are divided into two categories: T_SQL and CLR
T_SQL: Stored procedures are a collection of value-saved T_SQL statements that can accept and return user-provided parameters. Stored procedures may also be returned from the database to the client application data.
CLR stored procedures refer to method stored procedures that use the Microsoft.NET Framework common language. They can accept and return parameters provided by the user. They are implemented as public static methods of classes in the .NET Framework assembly.
3. Extended stored procedures
Extended stored procedures are implemented as dynamic connections (DLL files) executed outside the SQL SERVER environment. They can be loaded into the address space where the SQL SERVER instance is running and executed. Extended stored procedures can be executed using SQL SERVER extended stored procedure API programming, extended stored procedures are identified by the prefix "xp_". For users, extended stored procedures are the same as Mandarin stored procedures and can be executed in the same method.
Three: Create a stored procedure
If you want to do your job well, you must first sharpen your tools. Prepare the data as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Create a stored procedure without parameters
1 2 3 4 5 6 7 8 9 |
|
Modify the stored procedure
1 2 3 |
|
Delete the stored procedure
1 |
|
Rename the stored procedure
1 |
|
Create a stored procedure with parameters Stored procedures
The parameters of stored procedures are divided into two types: input parameters and output parameters
Input parameters: used to pass values into the stored procedure, similar to the value transfer in Java language or C.
Output parameters: used for meeting participation results after calling the stored procedure, similar to passing by reference in Java language.
The difference between transfer by value and transfer by reference:
Assignment to basic data types is transfer by value; assignment between reference types is transfer by reference.
Passing by value passes the actual variable value; passing by reference passes the reference address of the object.
After the value is passed, the two variables change their respective values; after the reference is passed, the two references change the state of the same object
(1) Stored procedure with one parameter
1 2 3 4 5 6 7 8 9 |
|
(2) With 2 parameters Stored procedure
1 2 3 4 5 6 7 8 9 10 11 |
|
(3) Create a stored procedure with a return value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
(4) Create a stored procedure with wildcards
1 2 3 4 5 6 7 8 9 10 11 |
|
(5) Encrypt the stored procedure
with encryption clause to hide the text of the stored procedure from the user. The following example creates an encryption process, uses the sp_helptext system stored procedure to obtain information about the encryption process, and then attempts to obtain information about the process directly from the syscomments table.
1 2 3 4 5 6 7 8 9 10 |
|
(6). Do not cache the stored procedure
1 2 3 4 5 6 7 8 9 10 11 |
|
(7). Create a stored procedure with cursor parameters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
(8). Create a paging stored procedure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics









