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

边栏推荐
- 百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
- HUSTPC2022
- Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar
- 18_ Redis_ Redis master-slave replication & cluster building
- 面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
- Semantic segmentation learning notes (1)
- 【网络安全】网络资产收集
- 数据分析常见的英文缩写(一)
- Learn the method code example of converting timestamp to uppercase date using PHP
- Internet Explorer officially retired
猜你喜欢

Table responsive layout tips

17_ Redis_ Redis publish subscription

搭建自己的语义分割平台deeplabV3+

. Net again! Happy 20th birthday
![[c voice] explain the advanced pointer and points for attention (2)](/img/fb/515e25899bd9a2905ee63cb041934a.png)
[c voice] explain the advanced pointer and points for attention (2)

04_ 栈

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

How to find a sense of career direction

Build your own semantic segmentation platform deeplabv3+

LeetCode刷题——两整数之和#371#Medium
随机推荐
Niuke Practice 101
Tidb data migration tool overview
Engineer evaluation | rk3568 development board hands-on test
02_线性表_顺序表
04_ 栈
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
记一次面试
TiDB数据迁移场景综述
使用 TiUP 部署 TiDB 集群
How to solve the problem of database content output
.NET Core 日志系统
[solution] educational codeforces round 82
Mavn 搭建 Nexus 私服
21_ Redis_ Analysis of redis cache penetration and avalanche
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
Record an interview
N皇后问题的解决
数据分析常见的英文缩写(一)
Map introduction
04.进入云原生后的企业级应用构建的一些思考