What is a stored procedure?
It is a set of SQL statements to complete specific functions, which are compiled and stored in the database. Compile once, valid forever
Is transferred directly through the stored procedure name
The stored procedure contains
- Program flow
- logic
- Query to the database
range
- parameter
- Output parameters
- Return single or multiple result sets
- return value
Why use stored procedures?
benefit
- reuse
- Reduce the workload of developers
- Improve efficiency
Types of stored procedures
- User-defined stored procedure
- System Stored Procedure
- Extended stored procedure
Rule
1. Objects created in the same stored procedure
can be referenced 2. Temporary tables can be referenced within the stored procedure
3. The called stored procedure can access all objects created by the first stored procedure
4. The remote stored procedure does not participate in transaction processing
5. The maximum number of parameters is 2100.
6. The maximum number of local variables is only limited by the available memory.
7. The available memory is different. The stored procedure can be up to 128MB.
Cannot include T-SQL statement to
create a stored procedure
Syntax format
Example
Use default value parameters
Use output parameters output
Use the output keyword to specify output parameters, you can directly add specific values to
update the stored procedure to
modify the stored procedure
Syntax format
Example
programmable environment- Find the stored procedure, click Modify, and edit to
delete the stored procedure.
Syntax format
Sample
system stored procedure
Method
- exec sp_who sa
- exec sp_helpdb
- exec sp_monitor
Temporary stored procedure
#Local Temporary Storage Procedure
##Global Temporary Storage Procedure
Example
If you create a new query and make the above call, it will show that the stored procedure cannot be found, because it is a local temporary stored procedure.
If you want to create a global temporary stored procedure, you need to delete the local temporary stored procedure
nested stored procedure. You
can nest up to 32 levels to
view the stored procedure.
Method
1. Programmability-write stored procedure script-create to-new query editor window (if you want If you save as a file, select the file at the end)
2. Use the system storage process
to directly save as a file