当前位置:网站首页>SQL server stored procedures multi angle introduction suggestions collection
SQL server stored procedures multi angle introduction suggestions collection
2022-07-27 18:53:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
What is stored procedure :
stored procedure (Procedure) Be similar to C# Methods in language , It is SQL Statement and control flow statement . Stored procedures are stored in the database , Can be executed by the application through a single call , And allow users to declare variables 、 Logic control statements and other powerful programming functions .
Stored procedures can contain logical control statements and data operation statements , It can receive parameters 、 Output parameters 、 Returns a single or multiple result sets and return values .
Stored procedures are compiled on the server when they are created , So the execution is more than a single SQL Fast sentence .
Be similar to C# Class library in ,SQL Server Some precompiled stored procedures are provided , These stored procedures are called “ System stored procedure ”.
SQL Server The characteristics of stored procedures in are as follows :
1> Receive input parameters , And return a value to the calling procedure or statement .
2> Contains programming statements that perform operations in the database or call other stored procedures .
3> Return the status value to the calling procedure , Indicates whether the execution process was successful ( If you fail , Also return the failure reason )
Using stored procedures has the following advantages :
1> Allow modular programming
Just create the stored procedure once and store it in the database , Therefore, the procedure can be called any time in the program .
2> Allow faster execution
If an operation requires a lot of T-SQL Code may need to be executed repeatedly , Stored procedures will be T-SQL The execution of batch code should be fast . Stored procedures will be analyzed and optimized when they are created , And you can use the version in memory of the process after the first execution of the process . But if T-SQL Batch code , Each run T-SQL When the sentence is , It should be sent repeatedly from the client , And in SQL-Server Every time these statements are executed , It should be compiled and optimized .
3> Reduce network traffic
After creating and using stored procedures , One needs hundreds of lines T-SQL The operation of the code , It can be realized by a single statement that executes the procedure code , Instead of sending hundreds of lines of code over the network
4> It can be used as a security mechanism
Even for users who do not have permission to directly execute statements in stored procedures , They can also be granted permission to execute the stored procedure
There are two types of stored procedures :
1> System stored procedure
2> User defined stored procedures
Common system stored procedures :
sp_databases: List all databases on the server
sp_helpdb: Reports information about the specified database or all databases
sp_renamedb: Change the name of the database
sp_tables: Return the list of objects that can be queried in the current environment
sp_columns: Return the information of a table column
sp_help: View all the information of a table
sp_helpconstraint: Look at the constraints of a table
sp_helpindex: Look at the index of a table
sp_stored_procedures: List all stored procedures in the current environment
sp_password: Add or modify the password of the login account
sp_helptext: Show default 、 Unencrypted stored procedures 、 User defined stored procedures 、 The actual text of the trigger or view
in addition , There is also a commonly used extended stored procedure :xp_cmdshell, It can do DOS Some operations under the command , Such as creating folders 、 List documents, etc .
http://www.cnblogs.com/roucheng/
User defined stored procedures :
In addition to system stored procedures , Users can also create their own stored procedures , You can use Microsoft SQL Management Studio Or use T-SQL sentence , Use Microsoft SQL Management Studio The steps to create a stored procedure are similar to views , Used to create stored procedures T-SQL Statement for CREATE PROCEDURE. All stored procedures are created in the current database .
Create a stored procedure without parameters :
Use T-SQL The syntax of creating a stored procedure with the statement is as follows :
CREATE PROC[EDURE] Stored procedure name
AS
SQL sentence
Calling stored procedure :
EXEC Stored procedure name
Create a stored procedure with input parameters :
Input parameters : You can pass parameters to the stored procedure at call time , Such parameters can be used to pass values in stored procedures .
Stored procedure with input parameters T-SQL The statement is as follows :
CREATE PROC[EDURE] Stored procedure name
@ Parameters 1 data type [ = The default value is ],
……,
@ Parameters n data type [ = The default value is ]
AS
SQL sentence
Calling stored procedure :
EXEC Stored procedure name Parameters 1 Value ,……, Parameters n Value
or
EXEC Stored procedure name @ Parameters 1= value ,……,@ Parameters n= value
Create a stored procedure with output parameters :
Output parameters : If you want to return a value , You can use the output parameter , There are after the output parameters “OUTPUT” Mark , After executing the stored procedure , The return value will be stored in the output parameter , For other purposes T-SQL Statement read access .
Stored procedures with output parameters T-SQL The grammar is as follows :
CREATE PROC[EDURE] Stored procedure name
@ Parameters 1 data type [ OUTPUT ],
……,
@ Parameters n data type [ OUTPUT ]
AS
SQL sentence
Calling stored procedure :
EXEC Stored procedure name Variable name OUTPUT
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/120712.html Link to the original text :https://javaforall.cn
边栏推荐
- 飞机大战敌机出场
- A case to understand MySQL view
- Wechat applet obtains mobile number
- Nacos display service registration address error
- filebeat.yml配置文件关于多个服务的配置问题
- Commodity comment information and comment information classification
- Order timeout cancellation and commodity query by category
- 文件的上传和下载
- 瑞吉外卖sql表
- LED学习护眼台灯触摸芯片-DLT8T10S-杰力科创
猜你喜欢

全自动吸奶器芯片-DLTAP703SD

瑞吉外卖笔记

V-bind and V-for

Run the uniapp to the mobile phone (real machine debugging)

网红RGB镜子灯触摸芯片-DLT8S15B-杰力科创

商品推荐和分类商品推荐

Using Jieba and pyhanlp tools to extract keyword words and sentences

迷你洗衣机触摸芯片-DLT8MA12TS-杰力科创
![[NPM] the](/img/ae/efccefae0323a1f6a425523e01d2ac.png)
[NPM] the "NPM" item cannot be recognized as the name of cmdlets, functions, script files or runnable programs. Please check the spelling of the name. If the path is included, make sure the path is co

MySQL 05 存储过程
随机推荐
LED带风扇护眼学习台灯触摸芯片-DLT8S12A
Led with fan eye protection learning table lamp touch chip-dlt8s12a
Order submission
Examples of map search
Vue uses keep alive to realize page caching
Uniapp H5 cross domain problem
ValueError: Found input variables with inconsistent numbers of samples: [80019456, 26673152]【报错】
20000 words + 30 pictures | what's the use of chatting about MySQL undo log, redo log and binlog?
I'm stupid. When completable future is used with openfegin, it even reports an error
filebeat.yml配置文件关于多个服务的配置问题
nacos显示服务注册地址错误
TypeError: conv2d(): argument ‘padding‘ (position 5) must be tuple of ints, not str【报错】
Order timeout cancellation and commodity query by category
JS to realize simple form verification and select all functions
Here are all the MySQL interview questions you can't expect (the latest version of 2022)
MySQL set validate_ Adding skip grant tables after password=off failed to start the service
Using Jieba and pyhanlp tools to extract keyword words and sentences
JS中的数组与对象
EN 1155 building hardware swing door opener - CE certification
TypeScript安装