当前位置:Gxlcms > mysql > 1107SQLserver基础

1107SQLserver基础

时间:2021-07-01 10:21:17 帮助过:11人阅读

【随堂练习】--查询李数老师教的数学成绩大于80分的学生的信息, 并且人数大于3的话,输出达标;否则输出不达标。 存储过程 --带参数的程序代码块---代表执行命令存储在数据库中,存储代码,没有调用 1、--创建存储过程 createprocedure--(可简写成proc)--+

【随堂练习】--查询‘李数’老师教的数学成绩大于80分的学生的信息, 并且人数大于3的话,输出达标;否则输出不达标。

存储过程 --带参数的程序代码块---代表执行命令存储在数据库中,存储代码,没有调用

1、--创建存储过程

create procedure--(可简写成proc) --+存储过程名称 -----存储过程格式

@bianliang1 --+数据类型 ---- as和存储过程名之间可以加参数形参(传递的形参)

@bianliang2 --+数据类型

as

Begin

可定义变量----临时用的形参

语句

End

2、--执行存储过程:

方法1:可编程性--存储过程--执行存储过程

默认自动的定义变量接受返回值。

【注意】不管成功还是失败,都会自带成功或失败的返回值,

返回0代表执行成功。

方法2:如何写代码执行存储过程

Exec(执行,execute) procedure

【注意】写代码同方法1的区别,是因为没有定义变量接受返回值就不会自动返回,想要得到变量结果,需要定义变量来接收存储过程才能打印。

3、--修改存储过程

Alter proc firstproc ------同修改表的格式

As

Select code,chinese,math,english ,name from score,student where score,student--写一句存储过程( 查询一个表)可以不写begin..end

go

Exec firstproc

4、--查询多个表必须加begin..end

create proc secondproc
as
begin
select *from score
select *from student
select *from teacher
end
go
exec secondproc

5、--加语句的存储过程

6、--带参数的存储过程(重点)

格式:

create proc fourproc --创建存储过程
@name varchar(20) --有多个变量,数据类 型后加逗号 --可加参数
as
begin --begin相当于大括号的左边
declare @counts int,@kecheng varchar(20) --定义临时变量+数据类型
select @counts=count(*) from teacher where name=@name --语句执行
if @counts=0 --加入分支语句
begin
print '没有找到这个老师'
end
else
begin
select @kecheng=course from teacher where name=@name
declare @count int
if @kecheng='语文'
begin
select @count=count(*) from score where stucode in (
select code from student where chteacher=(select code from teacher where name=@name)
) and chiese>=80
end
if @kecheng='数学'
begin
select @count=count(*) from score where stucode in (
select code from student where mateacher=(select code from teacher where name=@name)
) and math>=80
end
if @kecheng='英语'
begin
select @count=count(*) from score where stucode in (
select code from student where enteacher=(select code from teacher where name=@name)
) and english>=80
end
if @count>=3
begin
print '达标' --打印存储过程
end
else
begin
print '不达标'
end
end
end
go
exec fourproc @name='莫言1'

【随堂练习】

7、--使用return返回值得存储过程

【随堂练习1】

【随堂练习2】

8、--带返回值、返回参数、输入参数的存储过程

9、--删除存储过程

Drop proc fiveproc (+存储过程名称)

【课后练习】 -- 练习题目--
/*
创建一个货物表:编号,货物名称,单位,价格,库存数量,备注
新增10条数据
之后,进货,如果已有此货,增加数量,否则,新增入数据库表中
出货,如果有人要货,判断数量是否充足,充足减库存,否则告诉不足
*/

人气教程排行