当前位置:网站首页>Detailed explanation of SQL server stored procedures
Detailed explanation of SQL server stored procedures
2022-07-05 08:02:00 【Game programming】
One : Overview of stored procedures
1.1 The concept of stored procedure
stored procedure (Stored Procedure) It's precompiling SQL Statement set , These statements are stored in a name ( The name of the stored procedure ) And deal with it as a unit . Stored procedures replace the traditional one by one execution SQL How statements work , A stored procedure can contain queries 、 Insert 、 Delete 、 A series of updates and other manipulation SQL sentence , When this stored procedure is called and executed , These operations will also be performed at the same time .
Stored procedures are similar to procedures in other programming languages , It can take input parameters , And returns multiple values to the calling procedure or batch in the form of output parameters ; Contains information for performing operations in the database ( Including calling other procedures ) Of becomes a statement ; Returns the status value to the calling procedure or batch , To indicate success or failure ( And the reasons for the failure ).
SQL Server There are three types of stored procedures
1. System stored procedure : Used to manage SQL Server With stored procedures that display information about databases and users .
2. Custom stored procedures : The user is in SQL Server By using SQL Statement to create a stored procedure .
3. Extended stored procedure : Through programming language ( for example C) Create external routines , And put this routine in SQL Server Is used as a stored procedure in .
1.2 Advantages of stored procedures
Some aspects of stored procedures are as follows :
(1) Stored procedures can be nested , Support code reuse ;
(2) Stored procedures can accept and use parameters to dynamically execute SQL sentence ;
(3) Stored procedures are better than ordinary SQL Statement execution speed is fast . Stored procedures are compiled at the time of creation , Every time it is executed , No need to recompile . and SQL Statements need to be compiled every time they are executed ;
(4) Stored procedures have security features ( For example, authority ) And ownership Links , And certificates that can be attached to them . Users can be authorized to execute stored procedures without having to directly have permissions on the objects referenced in the stored procedures ( It's kind of like JAVA Encapsulation in object-oriented ).
(5) Stored procedures allow modular programming . Once the stored procedure is created , Then you can call any time in the program . This can improve the maintainability of the application ( It's kind of like JAVA The idea of layered architecture in ), And allow the application to agree to access the database .
(6) Stored procedures can reduce network traffic . One needs hundreds of lines SQL The operation of statement code can be executed by a statement executing procedure code , Instead of sending hundreds of lines of code over the network .
(7) Stored procedures can enforce application security . Parameterized stored procedures help protect applications from SQL Injection attack .
explain :SQL Injection It's an attack , It can insert malicious code that will later be passed to SQL Server String for analysis and execution . Any composition SQL Statements should be checked for injection vulnerabilities , because SQL Server All syntactically valid queries received by the executor .
Two : Create stored procedure
2.1 Use visualization tools to create stored procedures
Every visualization tool may have some differences , So what tools are used to create stored procedures , There should be a lot of resources to find in Baidu. There is no demonstration here
2.2 Use create proc Statement to create a stored procedure
The grammar is as follows
create proc Procedure Proc_Student
@Proc_Son int
as
select * from Student where Sno = @Proc_Son
3、 ... and : Manage stored procedures
3.1 Execute stored procedures
After the stored procedure is created , Can pass EXECUTE Command execution , It can be abbreviated as EXEC.
1.EXECUTE
EXECUTEyonglai zhixing Transact-SQL Command string in 、 character string 、 Or execute one of the following modules : System stored procedure 、 User defined stored procedures 、 Scalar valued user-defined functions or extended stored procedures .
2. Use EXECUTE Execute stored procedures
For example, executing stored procedures Proc_Stu
exec Proc_Stu
3.2 View stored procedures
1. Use sys.sql_modules View stored procedure definitions ;
select * from sys.sql_modules
2. Use OBJECT_DEFINITION View stored procedure definitions
OBJECT_DEFINITION(object_id)
3. Use sp_helptext View the definition of a stored procedure
Display the definition of user-defined rules 、 The default value is 、 Unencrypted Transact-SQL stored procedure 、 User defined Transact-SQL function 、 trigger 、 Calculated column 、CHECK constraint 、 View or system object ( Such as system stored procedures ). The statement is as follows :
sp_helptext 'Proc_Stu'
3.3 Modify stored procedure
Modifying a stored procedure can change the parameters or statements in the stored procedure , Can pass SQL Statement ALTER PROCEDURE Statements for . Although delete and recreate the stored procedure , You can also achieve the goal of modifying stored procedures , However, all permissions associated with the stored procedure will be lost .
1.ALTER PROCEDURE sentence
ALTER PROCEDURE Statement is used to modify by executing CREATE PROCEDUR Statement creation procedure . When this statement modifies a stored procedure , Permissions will not be changed , It doesn't affect related stored procedures or triggers .
example : Change the name to “Proc_Stu” Stored procedure .
ALTER PROCEDURE [dbo].[Proc_Stu]
@Son varchar(10)
as
select * from student
3.4 Rename stored procedure
sp_rename 'Proc_Stu','Proc_StuInfo'
Be careful : Changing any part of the object name can break scripts and stored procedures . It is not recommended to use this statement to rename stored procedures 、 trigger 、 User defined functions or views ; Instead, delete the object , Then recreate the object with a new name .
3.5 Delete stored procedure
DROP PROCENDURE Proc_Student
————————————————
Link to the original text :https://blog.csdn.net/little_jecklove/article/details/108990153
author : either progress or be washed backward , move forward , or you 'll fall behind
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- Relationship between line voltage and phase voltage, line current and phase current
- Halcon's practice based on shape template matching [2]
- C WinForm [help interface - send email] - practice five
- C # joint configuration with Halcon
- Extended application of single chip microcomputer-06 independent key
- Global and Chinese market of urban rail connectors 2022-2028: Research Report on technology, participants, trends, market size and share
- Consul installation
- C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,LU分解(LU Decomposition)源程序
- Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
- Altium designer 19.1.18 - hide the fly line of a network
猜你喜欢

Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic

Hardware 3 -- function of voltage follower

A simple method to prove 1/t Fourier transform

C language enhancement -- pointer

Software designer: 03 database system

导电滑环磨损快的原因

Altium Designer 19.1.18 - 更改铺铜的透明度

Drive LED -- GPIO control

MySQL blind note common functions
![[untitled] record the visual shock of the Winter Olympics and the introduction of the display screen](/img/43/7f8becc09c5ce7fe401bed140608f3.jpg)
[untitled] record the visual shock of the Winter Olympics and the introduction of the display screen
随机推荐
研究發現,跨境電商客服系統都有這五點功能!
Summary -st2.0 Hall angle estimation
Altium designer 19.1.18 - clear information generated by measuring distance
Altium Designer 19.1.18 - 更改铺铜的透明度
Live555 push RTSP audio and video stream summary (I) cross compilation
Train your dataset with yolov4
Development tools -- gcc compiler usage
找不到实时聊天软件?给你推荐电商企业都在用的!
OLED 0.96 inch test
A simple method to prove 1/t Fourier transform
Win10 shortcut key
The browser cannot access Baidu
Verilog -- state machine coding method
Shell脚本基本语法
C WinForm [exit application] - practice 3
Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
[untitled] record the visual shock of the Winter Olympics and the introduction of the display screen
mysql 盲注常见函数
Global and Chinese market of resistivity meter 2022-2028: Research Report on technology, participants, trends, market size and share
[popular science] some interesting things that I don't know whether they are useful or not