当前位置:网站首页>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 .
边栏推荐
- The browser cannot access Baidu
- LED display equipment records of the opening ceremony of the Beijing Winter Olympics
- Bootloader implementation of PIC MCU
- H264 (I) i/p/b frame gop/idr/ and other parameters
- Step motor generates S-curve upper computer
- Detailed explanation of pragma usage
- 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
- mysql 盲注常见函数
- C language # and #
猜你喜欢
C # joint configuration with Halcon
研究发现,跨境电商客服系统都有这五点功能!
Hardware and software solution of FPGA key chattering elimination
C WinForm [exit application] - practice 3
VESC Benjamin test motor parameters
导电滑环磨损快的原因
C language uses arrays to realize the intersection, union, difference and complement of sets
Train your dataset with yolov4
Summary -st2.0 Hall angle estimation
Shell script basic syntax
随机推荐
Global and Chinese market for blood typing 2022-2028: Research Report on technology, participants, trends, market size and share
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
Altium designer 19.1.18 - change the transparency of copper laying
Extended application of single chip microcomputer-06 independent key
L'étude a révélé que le système de service à la clientèle du commerce électronique transfrontalier a ces cinq fonctions!
Consul安装
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
Semiconductor devices (I) PN junction
导电滑环磨损快的原因
Create inf module in AMI code
Bluetooth hc-05 pairing process and precautions
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
Record the visual shock of the Winter Olympics and the introduction of the screen 2
Live555 push RTSP audio and video stream summary (I) cross compilation
Development tools -- gcc compiler usage
Global and Chinese markets for medical oxygen machines 2022-2028: Research Report on technology, participants, trends, market size and share
Fundamentals of C language
Beijing Winter Olympics opening ceremony display equipment record 3
Screen record of the opening ceremony of the Beijing winter olympics 2