当前位置:网站首页>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'

边栏推荐
- 搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
- The past and present lives of visual page building tools
- TiDB混合部署拓扑
- . Net again! Happy 20th birthday
- 编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
- 10_ Redis_ geospatial_ command
- TiDB数据迁移场景综述
- The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
- .NET Core 日志系统
- 学习使用php实现公历农历转换的方法代码
猜你喜欢

Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568

02_线性表_顺序表

. Net again! Happy 20th birthday

03_ Linear table_ Linked list

There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant

List set & UML diagram

21_Redis_浅析Redis缓存穿透和雪崩

The past and present lives of visual page building tools

Base64 coding can be understood this way

百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
随机推荐
[c voice] explain the advanced pointer and points for attention (2)
18_Redis_Redis主从复制&&集群搭建
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
Topology architecture of the minimum deployment of tidb cluster
php获取数组中键值最大数组项的索引值的方法
TiDB 环境与系统配置检查
Sharp tool SPL for post SQL calculation
Internet Explorer officially retired
Evaluation of embedded rz/g2l processor core board and development board of Feiling
Tidb data migration tool overview
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
03_線性錶_鏈錶
04.进入云原生后的企业级应用构建的一些思考
Application of CDN in game field
2021-2022学年编译原理考试重点[华侨大学]
21_ Redis_ Analysis of redis cache penetration and avalanche
JVM architecture, classloader, parental delegation mechanism
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
16_ Redis_ Redis persistence