当前位置:网站首页>SQL stored procedure
SQL stored procedure
2022-07-02 15:27:00 【slb190623】
Method statement :
Method name Parameters call Return value
The parameters are mainly one-to-one corresponding Dean
1. Corresponding types : A child class can replace a parent class int-dount
2. The number of corresponding : The default value is 、 Variable parameters
3. Sequence correspondence : have access to Parameters : Value method call
Return value : adopt return Return value . But only a single value can be returned
Can pass Ref/out Extended method's " Return value "
void show( Parameters )
{
Custom local variables
Logical statement
}
Stored procedure and function creation are similar
Stored procedure syntax
go
create procedure usp_ Stored procedure name
( Shape parameter ) -- Parameters can be defined in this
as -- Equivalent to method body
{
– Custom local variables
– Logical statement
}
go
Example 1
Create stored procedure , Check all student information
if exists(select * from sysobjects where name = 'usp_getAllStuInfo') -- If stored procedure usp_getAllStuInfo There is , Just delete
drop proc usp_getAllStuInfo
go
create procedure usp_getAllStuInfo -- Create stored procedure usp_getAllStuInfo
as
select * from student
go
-- Calling stored procedure , Get all student information
exec usp_getAllStuInfo
Calling stored procedure usp_getAllStuInfo Result :
Example 2
Create stored procedure , Query the student information of the specified gender
if exists(select * from sysobjects where name = 'usp_getAllStuInfoBySex')
drop proc usp_getAllStuInfoBySex
go
create procedure usp_getAllStuInfoBySex
@sex char(1) -- Formal parameters only need to be declared , It's not a definition , So no need declare
as
select * from student where Gender = @sex
go
-- Calling stored procedure usp_getAllStuInfoBySex, Query male student information
Exec usp_getAllStuInfoBySex 1
Calling stored procedure usp_getAllStuInfoBySex Result :
Example 3
Create stored procedure , Query the student information of the specified gender and class ,( The stored procedure defaults to gender male )
-- Create stored procedure , Query the student information of the specified gender and class
if exists(select * from sysobjects where name = 'usp_getAllStuInfoBySexAndGradeId') -- If stored procedure usp_getAllStuInfoBySexAndGradeId Delete if it exists
drop proc usp_getAllStuInfoBySexAndGradeId
go
create procedure usp_getAllStuInfoBySexAndGradeId -- Create stored procedure usp_getAllStuInfoBySexAndGradeId
@sex char(1) = 1, -- Formal parameters only need to be declared , It's not a definition , So no need declare, Add commas between multiple formal parameters
@gradeName nvarchar(20)
as
declare @gradeId int = (select gradeid from grade where gradeName = @gradeName)
select * from student where Gender = @sex and GradeId = @gradeId
go
-- Execute stored procedures usp_getAllStuInfoBySexAndGradeId, Query the student information of the specified gender and class
Exec usp_getAllStuInfoBySexAndGradeId 1,' Social University 1'
Calling stored procedure usp_getAllStuInfoBySexAndGradeId , The result of specifying gender :
Calling stored procedure usp_getAllStuInfoBySexAndGradeId , The result of not formulating gender :
-- The parameters are passed in the same order : The first argument corresponds to the first parameter ... By analogy
-- If there is a default value , have access to default
-- You can also use Parameters = value Call stored procedures in a way ( Note that once invoked in this way , Then the subsequent parameters must also use this method , Otherwise, the report will be wrong , The previous parameters are irrelevant ), This has nothing to do with order
Exec usp_getAllStuInfoBySexAndGradeId default,' Social University 1'
-- perhaps
Exec usp_getAllStuInfoBySexAndGradeId @gradeName =' Social University 1'

边栏推荐
猜你喜欢

Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?

02_ Linear table_ Sequence table

. Solution to the problem of Chinese garbled code when net core reads files
![[noi simulation] Elis (greedy, simulation)](/img/a2/f8c8ab3bc8dd779327be3f76990976.png)
[noi simulation] Elis (greedy, simulation)

Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language

06_ Stack and queue conversion

yolo格式数据集处理(xml转txt)

Let your HMI have more advantages. Fet-g2ld-c core board is a good choice

kibana 基础操作

数据分析思维分析方法和业务知识——业务指标
随机推荐
Yolov5 code reproduction and server operation
Case introduction and problem analysis of microservice
06_栈和队列转换
04_ Stack
Key points of compilation principle examination in 2021-2022 academic year [overseas Chinese University]
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
AtCoder Beginner Contest 254
[solution] educational codeforces round 82
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
Practical debugging skills
Base64 coding can be understood this way
17_ Redis_ Redis publish subscription
XML Configuration File
工程师评测 | RK3568开发板上手测试
How does the computer set up speakers to play microphone sound
13_ Redis_ affair
Recommended configuration of tidb software and hardware environment
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
Learn the method code example of converting timestamp to uppercase date using PHP
Points clés de l'examen de principe de compilation pour l'année scolaire 2021 - 2022 [Université chinoise d'outre - mer]