第七章 课上练习

本文介绍了SQL Server中存储过程的应用,包括带参数的存储过程和带OUT参数的存储过程的具体实现方式。通过示例展示了如何根据不同的分数筛选学生信息以及如何进行分页查询。
------------------------带参数的存储过程--------------
--从result,student表中查询成绩大于60分的学生的 姓名和成绩
--及格线会调整
create procedure usp_unpass
@score int
as
if(@score<0 or @score>100)
begin
   print '错'
end
else
begin
  select StudentName,StudentResult
  from Student as a,Result as v
  where a.studentNo=v.studentNo
   and studentresult>@score
end 

exec usp_unpass 80

go
------------------------------带out参数的存储过程-----------------------------------
--分页存储 过程
--每页条数据
--查询第二页的数据 4-6

create procedure usp_GetPageListS2226
@pageIndex int,--第几页
@pagesize int,--页大小呢
@totalRecords int out,--总记录数
@totalPages int out--总页数
as
select @totalRecords=count(1) from student
select * from
(
select * ,row_number() over(order by studentno) as myid
 from student
 ) as temp
 where myid between (@pageIndex-1)*@pagesize+1 and @pageIndex*@pagesize
 declare @record int
 declare @page int
 exec usp_GetPageListS2226 2,3,@record out,@page out
 print @record
 print @page

 ---------------------------------------------------------------------------------
 --互联网是非常危险的(专门攻击他人电脑,肉鸡)。
 --小孩们不要随意接触互联网

 --1.什么是存储过程?
 --就是在数据库服务器上保存的一对预编译(:驻留SQL Server服务器的内存中。。。)的SQL

 --1.1存储过程分类
   --3类: 用户自定义存储过程(*****)usp_
   --      系统存储过程(sp_System PROCEDURE)
   --       扩展存储过程(xp_extends procedure)

   --2.如何使用存储过程

   go
   create procedure usp_StudentInfo
   as
   --增删改,,也可以是查询
   select * from student

   --------------------------------------------------
   --new  三件事
   --1:创建对象
   --2.调用构造函数
   --3.开辟空间
   ---------------------------------
   --EXEC sp_helpIndex name
   --EXEC sp_helpIndex Result

   --SELECT * FROM sys.indexes
   --USE MySchool
   --SELECt * FROM sys.indexes

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值