Getting to Know Stored Procedures

--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


-- Comparison of C# calling methods: GetStudentsInfoByGrade(70,90)

exec GetStudentsInfoByGrade @maxgrade = 90
go

exec GetStudentsInfoByGrade 70,90
go

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

-- 3rd generation output (out) parameter 
/*
public void GetStuAvgradeTotalAndFemaleGradeTotal(out decimal stuTotal, out decimal stuFemaleTotal)
*/ 
--The stored procedure has two types of parameters, one is input parameter and default value input, the other is output parameter called 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 =  ' female ' 
go

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


-- 4 Modify and encrypt the stored procedure 
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 ' , ' Li Gang ' , ' Male ' , ' 1987-07-01 ' , ' Network Engineering ' , 85.8 , ' Department of Information Engineering '   

select * from student

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325019116&siteId=291194637