当前位置:网站首页>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 .
边栏推荐
- How to define guid in AMI code
- L'étude a révélé que le système de service à la clientèle du commerce électronique transfrontalier a ces cinq fonctions!
- Record the torch encountered by win10 cuda. is_ False problem in available()
- Global and Chinese market of blackbody calibration source 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of urban rail connectors 2022-2028: Research Report on technology, participants, trends, market size and share
- Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
- Record the opening ceremony of Beijing Winter Olympics with display equipment
- 1089 insert or merge, including test point 5
- 通过sql语句统计特定字段出现次数并排序
- 研究发现,跨境电商客服系统都有这五点功能!
猜你喜欢

Nb-iot technical summary

Wifi-802.11 negotiation rate table

Altium designer 19.1.18 - Import frame

Acwing-宠物小精灵之收服-(多维01背包+正序倒序+两种形式dp求答案)

Some thoughts on extracting perspectives from ealfa and Ebeta

Ten thousand words detailed eight sorting must read (code + dynamic diagram demonstration)
![Halcon's practice based on shape template matching [1]](/img/68/206eed7502fbf108a929aa9365b1ae.jpg)
Halcon's practice based on shape template matching [1]

Network communication process

Can't find real-time chat software? Recommend to you what e-commerce enterprises are using!

LED display equipment records of the opening ceremony of the Beijing Winter Olympics
随机推荐
Shell脚本基本语法
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
Ads usage skills
Vofa+ software usage record
如何进行导电滑环选型
Carrier period, electrical speed, carrier period variation
Create inf module in AMI code
Hardware 1 -- relationship between gain and magnification
Record the opening ceremony of Beijing Winter Olympics with display equipment
Global and Chinese markets for recycled boilers 2022-2028: Research Report on technology, participants, trends, market size and share
Altium designer learning (I)
Altium designer 19.1.18 - clear information generated by measuring distance
Cadence learning records
Fundamentals of C language
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
PMSM dead time compensation
OLED 0.96 inch test
Wifi-802.11 negotiation rate table
WiFi wpa_ Detailed description of supplicant hostpad interface
UEFI development learning series