Use new
go
select *from xuesheng
select *from fenshu
select *from jiaoshi
==============================================
--查‘赵文‘教的语文成绩>80 的学生信息,如果人数超过个,考核达标
----正确的写法
declare @count int
select @count=count(*) from fenshu where code in (
select code from xuesheng where yuhao =(select code from jiaoshi where name=‘赵文‘)
) and yufen >=80
print @count
if @count>3
print ‘达标‘
else
print ‘不达标‘
==================================================
create proc firstproc
as
select *from fenshu
go --好比C#中创建的函数,要引用才起作用!
--在数据库的存储过程文件夹里,找到保存的存储过程,右键单击打开‘执行存储过程‘就会完成执行.
--并返回值,0代表完成!
declare @fanhuizhi int
execute @fanhuizhi=firstproc --查询
--或者exec firstproc 也可以执行firstproc 存储过程。
--print @fanhuizhi --在结果中打印出表格,在消息中显示.
select @fanhuizhi --在结果中显示两个表,一个表示查询的表,另一个是返回值!
-----------------------修改存储过程-----------------------
alter proc firstproc
as
select fenshu.code ,yufen,name from fenshu,xuesheng
where fenshu.code =xuesheng.code
go
exec firstproc
----------------------查询多个表-------------------------
create proc secondproc
as
begin
select *from xuesheng
select *from fenshu
end
go
execute secondproc
-----------------------含参数的存储过程-----------------------------
create proc thirdproc
@name varchar(20)
as
begin
declare @jiaoshicode int, @kecheng varchar(20)
select @jiaoshicode ,@kecheng from jiaoshi where name [email protected]
if @kecheng =‘语文‘
begin
select @count=count(*) from fenshu where code in (
select code from xuesheng where yuhao =(select code from jiaoshi where name=‘赵文‘)
end
if @kecheng=‘数学‘
begin
select @count=count(*) from fenshu where code in (
select code from xuesheng where shuhao =(select code from jiaoshi where name=‘李数‘)
end
if @kecheng =‘英语‘
begin
select @count=count(*) from fenshu where code in (
select code from xuesheng where yinghao =(select code from jiaoshi where name=‘张外‘)
end
end
go
exec thirdproc ‘李数‘-- ‘李数‘(教师表中的name)是执行存储过程的变量值!
------------------------------------------------------
create proc fouthproc
@hello varchar(20)
as
begin
print @hello
end
go
exec fouthproc ‘今天是个好天气‘—表示变量@hello被赋值‘今天是个好天气’--注意书写流程!
----------------输入学号,判断学生结业--------------------------------------
create proc biye
@xuehao int----------创建变量. 就是需要输入值去赋值的变量
as
begin
declare @count int
select @count=COUNT(*) from xuesheng where [email protected]
if @count =0
print ‘请检查输入的学号是否有误!‘
else
begin
declare @yu int
select @yu=COUNT(*) from fenshu where [email protected] and yufen>=60
declare @shu int
select @shu=COUNT(*) from fenshu where [email protected] and shufen>=60
declare @ying int
select @ying=COUNT(*) from fenshu where [email protected] and yingfen>=60
declare @he int
set @[email protected][email protected][email protected]
if @he=3
print ‘优秀!已经结业!‘
if @he=2
print ‘已经结业!‘
if @he=1
print ‘不能结业!请重修两门课程‘
if @he=0
print ‘请重修所有课程!‘
end
end
go
exec biye 1 --输入code值是 1,表示查询存储过程biye
drop proc biye --删除创建的存储过程biye
------------------使用return接收---------------------------------------
create proc jieshou
@hao int----------创建变量. 就是需要输入值去赋值的变量
as
begin
set @[email protected]+10 --赋值!
return @hao
end
go --存储过程完成,return接收,但是没有打印
declare @abc int --打印结果要创建新变量,打印!
exec @abc=jieshou 3 --写 存储过程 名字!
print @abc --当变量@hao=3时,存储过程jieshou, return的结果 13
-------------------使用return 累加求和-------------------------
create proc leijia
@n int
as
begin
declare @sum int=0 ,@a int =0 --创建,并且赋值为
while @a <[email protected] --while循环
begin
set @sum [email protected][email protected]
set @[email protected]+1 --赋值时一定要 set
end
return @sum
end
go
declare @haha int
exec @haha=leijia 5
print @haha --输出5的累加 15
--------输出多变量情况 (return、output 接收方法)-----------
craete proc sixproc --做修改后,要变成alter proc sixproc
@code int ,
@yu decimal(18,2) output,
@shu decimal(18,2) output,
@ying decimal(18,2) output
as
begin
declare @count int
select @count=COUNT (*) from fenshu where [email protected]
select @yu=yufen ,@shu=shufen,@ying=yingfen from fenshu where [email protected]
return @count --运行到return语句,就结束语句执行。所以一般在最后或循环里面
end
go
declare @yu decimal(18,2),@shu decimal(18,2),@ying decimal(18,2),@count int
--创建对应的 接收 变量
exec @count=sixproc 1 (表示查询的方式),@yu output ,@shu output,@ying output
--(output的接收方法)
print @yu [email protected] [email protected] --表示code是1 的语数外总和!
print @count --此时返回1.表示个数!