SQL 存储过程使用

use master;    --切换到master数据库
go

create database stuDB
go

use stuDB    --选择当前数据库
go

CREATE TABLE StuInfo --创建学生信息表
(
    StuNo varchar(12) primary key,
    StuName varchar(20) not null ,
    StuAge int check(StuAge>=0 and StuAge<=120),
    StuSex char(2) check(StuSex='' or StuSex='') default(''),
    StuTel varchar(15),
    StuAddress varchar(50) default ('地址不详'),
    StuEmail varchar(50),
    birthday date,
    classname varchar(12)
)
go

CREATE TABLE Exam --创建学生成绩表
(
    ExamNo int primary key identity(1,1),
    StuNo varchar(12) foreign key references StuInfo(StuNo),
    Written decimal(10,2) check(Written>=0 and Written<=100),
    Lab decimal(10,2) check(Lab>=0 and Lab<=100)
)
go

 

 


--插入数据
insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135809010281','周文学','90-03-17','2222273','湖南衡阳','Y2T116');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135507150062','陈辉煌','82-10-10','2222274','江西全南县','Y2T48');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135508080033','曾华军','86-08-22','2222275','广东珠海','Y2T49');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135512190343','罗燕','83-11-07','22222746','云南省潞西市','Y2T65');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135508150034','黄志伟','87-11-13','22222746','江西会昌县','Y2T50');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135508150045','涂乾文','86-07-22','2228852','广东珠海','Y2T50');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135508150156','唐宁','81-07-01','22222746','广东珠海','Y2T50');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135508260037','贝然','85-06-13','22222746','广东珠海','Y2T60');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135508080198','白生全','86-06-03','8828888','重庆','Y2T49');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135409250289','沈永强','81-06-23','22222746','江苏南通市','Y2T50');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('217507100106','张键','85-07-18','8557378','广东江门','Y2T50');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135509060163','吴家彬','82-09-22','8324282','珠海市斗门区','Y2T52');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135509120044','邹城廷','85-10-15','8218358','广东珠海','Y2T53');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135407060145','娄智欣','85-09-21','8545256','江西峡江','Y2T42');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135503040102','陈迪华','00-01-01','5612698','广东珠海','Y2T42');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135503210135','陈福清','80-11-25','22222746','江西于都县','Y2T42');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135512280012','陶正武','83-06-19','22222746','湖南益阳市','Y2T42');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135406070146','杨振杰','75-08-10','22222746','珠海','Y2A21');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135504180237','鲁力','82-03-23','22222746','广东珠海','Y2T44');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135505130263','贺际勇','80-11-06','2628682','湖南常德市','Y2T44');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('514512260014','刘峰','82-02-06','8385613','湖南衡阳市','Y2T44');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135505310242','陈学斌','82-02-03','8358801','广东珠海','Y2T45');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135505310012','郑银河','81-08-26','22222746','湖南隆回县','Y2T47');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135507150142','陈卓','86-08-30','2210387','江西省于都县','Y2T47');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135507150193','贾海波','85-04-10','22222747','湖南蓝山县','Y2T47');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135509120063','钟文康','84-05-15','22222749','广东珠海市','S2T53');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135509120073','朱婉婷','83-08-11','22222743','广东珠海','Y2T53');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135510110082','易虎','84-02-28','22222744','四川安岳县','Y2T56');


insert into stuinfo (stuno,stuName,birthday,stutel,stuAddress,classname)
values('135510110132','陈挺','84-05-19','22222745','珠海香洲','S2T56');



--插入成绩表
insert into Exam values('135510110132',95,86);
insert into Exam values('135510110082',55,56);
insert into Exam values('135509120073',65,86);
insert into Exam values('135509120063',60,60);
insert into Exam values('135507150193',89,null);
insert into Exam values('135507150142',91,60);
insert into Exam values('514512260014',60,76);
insert into Exam values('135505130263',null,null);
insert into Exam values('135504180237',null,86);
insert into Exam values('135406070146',54,46);
insert into Exam values('135512280012',46,76);
insert into Exam values('135503210135',32,86);
insert into Exam values('135503040102',86,36);

--查询数据
select * from stuinfo
select * from Exam

 

--创建存储过程
create proc proc_stuInfo
as 
   select * from StuInfo
go

--调用存储过程
exec proc_stuInfo
go

--创建存储过程
create proc Proc_passStu
as
 select stuname,stuage,stusex,stuADDress,written,Lab
from  StuInfo s ,Exam e where s.StuNo=e.StuNo
go

--调用存储过程
exec Proc_passStu
go
 select stuname,stuage,stusex,stuADDress,written,Lab
from  StuInfo s ,Exam e where s.StuNo=e.StuNo


--创建带输入参数的存储过程
create proc proc_passStu1
@written int,
@Lab int
as
 select stuname,stuage,stusex,stuADDress,written,Lab
from  StuInfo s ,Exam e where s.StuNo=e.StuNo
and written>=@written and Lab>=@Lab
go

--调用带参存储过程
exec proc_passStu1 60,60
go

--创建带输入参数并赋默认值的存储过程
create proc proc_passStu2
@written int=60,
@Lab int=60
as
 select stuname,stuage,stusex,stuADDress,written,Lab
from  StuInfo s ,Exam e where s.StuNo=e.StuNo
and written>=@written and Lab>=@Lab
go


--调用存储过程
exec proc_passStu2
exec proc_passStu2 70,60

--笔试及格线为70,机试默认60
exec proc_passStu2 70

--笔试及格线默认60,机试及格线为70
exec proc_passStu2 default,70
exec proc_passStu2 @lab=70
go


--创建带输出参数的存储过程
create proc proc_passStu3
@passnum int output
as
   select @passNum=count(*) from Exam
    where Written>=60 and Lab>=60
go


declare @num int
exec proc_passStu3 @num output
print '本次及格人数:'+convert(varchar(5),@num)



--创建带输入参数并赋默认值及输出参数的存储过程
--没有默认值的参数定义在前面比较好
create proc proc_passStu4
@failnum int output,
@written int=60,
@Lab int=60
as
    declare @writtenavg decimal(5,2),@labavg decimal(5,2)
    select @written=AVG(written) from Exam where written is not null
    select @lab=AVG(Lab) from Exam where Lab is not null
    print '笔试平均分'+ convert(varchar(2),@written)
    print '机试平均分'+ convert(varchar(2),@lab)
    
    print '本班考成绩:'+
    case
        when @writtenavg>=70 and @labavg>=70 then '优秀'
        when @writtenavg>=60 and @labavg>=60 then '一般'
        else '较差'
    
    end
    
    print '-------------------------------------------------'
    print '           参加本次考试没有通过的学员'
    print '-------------------------------------------------'
    
        select s.StuName,e.StuNo,written,lab
        from Exam e,StuInfo s where e.StuNo=s.StuNo
        and (written<@written or lab <@Lab)

    select @failnum =count(*)from Exam
    where written<@written or lab <@Lab
    
go
declare @failnum int
exec  proc_passStu4 @failnum output 
print '本次不及格的人数:'+convert(varchar(5),@failnum)
go



--注意条件要加小括号
select StuName,Written,Lab 
from StuInfo s,Exam e
where s.StuNo = e.StuNo 
and (Written<60 or Lab<60)

--使用join就不用
select StuName,Written,Lab 
from StuInfo s join Exam e
on s.StuNo = e.StuNo
where Written<60 or Lab<60
posted @ 2019-03-08 15:28  随★风  阅读(17057)  评论(0编辑  收藏  举报