[Database video] Overview of stored procedures

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

  1. reuse
  2. Reduce the workload of developers
  3. 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
Insert picture description here
create a stored procedure
Syntax format
Insert picture description here
Example
Insert picture description here
Use default value parameters
Insert picture description here
Use output parameters output
Use the output keyword to specify output parameters, you can directly add specific values ​​to
Insert picture description here
update the stored procedure to
Insert picture description here
modify the stored procedure
Syntax format
Insert picture description here
Example
programmable environment- Find the stored procedure, click Modify, and edit to
Insert picture description here
delete the stored procedure.
Syntax format
Insert picture description here
Sample
Insert picture description here
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
Insert picture description here
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
Insert picture description here
Insert picture description here
nested stored procedure. You
can nest up to 32 levels to
Insert picture description here
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
Insert picture description here
to directly save as a file

Guess you like

Origin blog.csdn.net/weixin_45490023/article/details/111462721