当前位置:网站首页>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 .
边栏推荐
- Realization of binary relation of discrete mathematics with C language and its properties
- C language enhancement -- pointer
- Global and Chinese markets of nano biosensors 2022-2028: Research Report on technology, participants, trends, market size and share
- 生产中影响滑环质量的因素
- Create inf module in AMI code
- Bootloader implementation of PIC MCU
- The global and Chinese market of lithographic labels 2022-2028: Research Report on technology, participants, trends, market size and share
- Verilog -- state machine coding method
- 软件设计师:03-数据库系统
- How to define guid in AMI code
猜你喜欢
导电滑环磨损快的原因
Volatile of C language
Measurement fitting based on Halcon learning [i] fuse Hdev routine
Altium designer 19.1.18 - clear information generated by measuring distance
Semiconductor devices (I) PN junction
Network port usage
生产中影响滑环质量的因素
Altium designer learning (I)
Interview catalogue
A simple method to prove 1/t Fourier transform
随机推荐
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
The research found that the cross-border e-commerce customer service system has these five functions!
生产中影响滑环质量的因素
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine
Vofa+ software usage record
OLED 0.96 inch test
A simple method to prove 1/t Fourier transform
Factors affecting the quality of slip rings in production
How to excavate and research ideas from the paper
Shell脚本基本语法
Consul installation
Cadence learning records
Carrier period, electrical speed, carrier period variation
Global and Chinese market of resistivity meter 2022-2028: Research Report on technology, participants, trends, market size and share
Baiwen 7-day smart home learning experience of Internet of things
Relationship between line voltage and phase voltage, line current and phase current
研究发现,跨境电商客服系统都有这五点功能!
Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
Altium designer 19.1.18 - hide the fly line of a network
H264 (I) i/p/b frame gop/idr/ and other parameters