当前位置:网站首页>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'
边栏推荐
- 02_ Linear table_ Sequence table
- 损失函数与正负样本分配:YOLO系列
- HUSTPC2022
- Recommended configuration of tidb software and hardware environment
- Real estate market trend outlook in 2022
- How to find a sense of career direction
- Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
- 03_ Linear table_ Linked list
- CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
- Tidb data migration tool overview
猜你喜欢
随机推荐
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
11_ Redis_ Hyperloglog_ command
How to avoid 7 common problems in mobile and network availability testing
Tidb hybrid deployment topology
Map introduction
LeetCode刷题——去除重复字母#316#Medium
Mavn 搭建 Nexus 私服
Niuke Practice 101
yolo格式数据集处理(xml转txt)
Application and practice of Jenkins pipeline
【网络安全】网络资产收集
Mavn builds nexus private server
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
05_队列
工程师评测 | RK3568开发板上手测试
Application of CDN in game field
04_ 栈
LeetCode刷题——验证二叉树的前序序列化#331#Medium
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency