初识存储过程(代码)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初识存储过程(代码)相关的知识,希望对你有一定的参考价值。

--1
create procedure GetStudentsInfo
as
    select * from student
go


exec GetStudentsInfo

alter procedure GetStudentsInfo
as
    select * from student where stuSex = 

exec GetStudentsInfo

--2
/*
public DataTable GetStudentsInfoByGrade(decimal mingrade, decimal maxgrade)
{}
*/

create proc GetStudentsInfoByGrade
    @mingrade numeric(3,1) = 60,
    @maxgrade numeric(3,1)
as
    select * from student where stuAvgrade >= @mingrade and stuAvgrade <= @maxgrade
go


--C#调用方法的对比:GetStudentsInfoByGrade(70,90)

exec GetStudentsInfoByGrade @maxgrade = 90
go

exec GetStudentsInfoByGrade 70,90
go

exec GetStudentsInfoByGrade @mingrade = 80,@maxgrade = 90
go

--3代输出(out)参数
/*
public void GetStuAvgradeTotalAndFemaleGradeTotal(out decimal stuTotal, out decimal stuFemaleTotal)
*/
--储存过程有两种参数类型一种是输入参数也是默认值input另一种是输出参数叫output 
create procedure GetStuAvgradeTotalAndFemaleGradeTotal
    @stuTotal real output,
    @stuFemaleTotal real output
As
    select @stuTotal = sum(stuAvgrade) from student;
    select @stuFemaleTotal = sum(stuAvgrade) from student
    where stuSex = 
go

declare @total real, @femaleTotal real
exec GetStuAvgradeTotalAndFemaleGradeTotal @total output, @femaleTotal output;
select @total, @femaleTotal
go


--4修改和加密储存过程
alter procedure GetStudentsInfo with encryption
as
    select * from student
go

--5
create proc InsertStudent
    @stuId char(8),
    @stuName varchar(10),
    @stuSex  char(2),
    @stuBirth smalldatetime,
    @stuSpeciality varchar(50),
    @stuAvgrade numeric(3,1),    
    @stuDept  varchar(50) 
as
    insert into student values(@stuId,@stuName,@stuSex,@stuBirth,@stuSpeciality,@stuAvgrade,@stuDept)
go    


exec InsertStudent 20060214, 李刚,,1987-07-01,网络工程,85.8,信息工程系   

select * from student

 

以上是关于初识存储过程(代码)的主要内容,如果未能解决你的问题,请参考以下文章

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

Java初识方法

初识MyBatis

初识MyBatis

如何将代码片段存储在 mongodb 中?

初识 Sql Server存储过程