当前位置:网站首页>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 .
边栏推荐
- Shell脚本基本语法
- Global and Chinese market of quenching furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
- Shell script basic syntax
- The research found that the cross-border e-commerce customer service system has these five functions!
- Use of orbbec Astra depth camera of OBI Zhongguang in ROS melody
- C WinForm [display real-time time in the status bar] - practical exercise 1
- MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
- WiFi wpa_ Detailed description of supplicant hostpad interface
- Global and Chinese market of core pallets 2022-2028: Research Report on technology, participants, trends, market size and share
- Ads learning record (lna_atf54143)
猜你喜欢

Nb-iot technical summary

Train your dataset with yolov4

Beijing Winter Olympics opening ceremony display equipment record 3
![Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine](/img/a1/d13b37955b044b6be5f1fd10263c5e.jpg)
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
![C WinForm [get file path -- traverse folder pictures] - practical exercise 6](/img/8b/1e470de4e4ecd4fd1bb8e5cf23f466.jpg)
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
![Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine](/img/13/22a1915329f58acd54c40176f6f301.jpg)
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine

The printer encountered an abnormal configuration problem 0x8007007e (win10)

导电滑环磨损快的原因

Programming knowledge -- basis of C language

The research found that the cross-border e-commerce customer service system has these five functions!
随机推荐
General makefile (I) single C language compilation template
mysql 盲注常见函数
Some errors in configuring the environment
LED display equipment records of the opening ceremony of the Beijing Winter Olympics
Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine
About yolov3, conduct map test directly
Development tools -- gcc compiler usage
Volatile of C language
String judgment
How to select conductive slip ring
Network communication model -- Network OSI tcp/ip layering
C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
Use of orbbec Astra depth camera of OBI Zhongguang in ROS melody
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
Communication standard -- communication protocol
Halcon's practice based on shape template matching [1]
Programming knowledge -- assembly knowledge
2021-10-28