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

边栏推荐
- 18_ Redis_ Redis master-slave replication & cluster building
- YOLOV5 代码复现以及搭载服务器运行
- Data analysis thinking analysis methods and business knowledge - business indicators
- . Solution to the problem of Chinese garbled code when net core reads files
- How to conduct TPC-C test on tidb
- 学习使用php将时间戳转换为大写日期的方法代码示例
- 19_ Redis_ Manually configure the host after downtime
- 编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- 百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
猜你喜欢

Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!

15_Redis_Redis.conf详解

10_ Redis_ geospatial_ command

Base64 coding can be understood this way

YOLOV5 代码复现以及搭载服务器运行

损失函数与正负样本分配:YOLO系列

面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?

The past and present lives of visual page building tools

Pytorch 保存tensor到.mat文件

02_线性表_顺序表
随机推荐
21_ Redis_ Analysis of redis cache penetration and avalanche
Solve the problem that El radio group cannot be edited after echo
Tidb environment and system configuration check
16_Redis_Redis持久化
Solution of Queen n problem
Yolo format data set processing (XML to txt)
Record an interview
04_ 栈
php获取数组中键值最大数组项的索引值的方法
Jenkins Pipeline 应用与实践
12_Redis_Bitmap_命令
16_ Redis_ Redis persistence
Apprendre le Code de la méthode de conversion du calendrier lunaire grégorien en utilisant PHP
02_线性表_顺序表
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
搭建自己的语义分割平台deeplabV3+
21_Redis_浅析Redis缓存穿透和雪崩
[noi Simulation Competition] scraping (dynamic planning)
Tidb data migration tool overview
17_Redis_Redis发布订阅