存储过程
drop PROCEDURE usp_student --删除程序 usp_student --定义自己的第一个无参存储过程 --CREATE PROC[EDURE] 存储过程名 -- @参数1 数据类型 = 默认值 OUTPUT, -- … … , -- @参数n 数据类型 = 默认值 OUTPUT -- AS -- SQL语句 --GO --User Stored Procedure 用户自定义存储过程 -- 存信息的 --创建 程序 Create procedure usp_student as --例如 select *from Student use MySchool --定位数据库 --创建 程序 学生目录(存储过程名) create procedure usp_studentlist @scoreLine int --声明分数线 as --例如 select studentname,studentresult from student,result where student.studentno=result.studentno and studentresult>@scoreLine --学生成绩大于分数线 select *from Student--查询成绩表 --调用存储过程 exec usp_studentlist 92 --实行存储过程名大于90分 declare @score int --声明局部变量score set @score=92 --赋值 exec usp_studentlist @score --实行存储过程名 -- go alter procedure usp_studentlist --更改程序 存储过程名 @scoreLine int --声明分数线 as if(@scoreLine <0 or @scoreLine>100) --0<分数线<100 begin --开始 print '您输入的分数有误' --输出 end ---结束 else begin select studentname,studentresult from student,result where student.studentno=result.studentno and studentresult>@scoreLine end --调用存储过程 exec usp_studentlist 12 --实行存储过程名大于12分 -- --更改 程序 go create proc usp_getPageList --创建 程序 存储过程名 @pageIndex int, --当前是第几页 @pageSize int, --每页记录数 3 @totalPages int output--总页数 输出 as 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 --where myid between 1 and 3 --总页数=总记录数/pageSize declare @totalRecords int select @totalRecords=COUNT(1) from Student set @totalPages=CEILING(@totalRecords*1.0/@pageSize) ---调用 declare @pages int --页数 set @pages=0 exec usp_getPageList 1,3,@pages output --输出 print @pages select * from Student select 5*1.0/2 --天花板函数 select CEILING(1.00000001) --地板floor函数 向下取整 select FLOOR(1.9999999999999)