当前位置:Gxlcms > 数据库问题 > SQL Server存储过程

SQL Server存储过程

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

---------------1、创建自己的存储过程proc_helloworld-------------------------- create proc proc_helloworld --alter proc proc_helloworld --已生成的存储过程,如需修改,将create改成alert,选中执行重新生成 as begin print hello world end ---------鼠标选中以上内容,执行。生成存储过程。先生成再调用------ exec proc_helloworld --调用执行存储过程 -----------------2.1、创建带两个输入参数的存储过程proc_add----------------------- create proc proc_add @n1 int, --参数n1,整型 @n2 int --参数n2,整型 --@n2 int=10 --n2带默认值10 as begin select @n1+@n2 end ---------鼠标选中以上内容,执行。生成存储过程------ exec proc_add 3,5 --3+5 --exec proc_add @n1=3 --3+10 --exec proc_add @n1=3,@n2=11 --3+11,默认值10失效,以新赋值为准 -----------------2.2、创建带输出参数的存储过程proc_ShowStudent---------------- create proc proc_ShowStudent @gender char(2), @recordcount int output --输出参数,记录查到的条数 as begin select * from tb_Student where stGender=@gender set @recordcount=(select count(*) from tb_Student where stGender=@gender) --检索的结果条数赋值给recordcount end --调用执行存储过程: declare @rc int --声明变量rc,用来存储recordcount的值 exec proc_ShowStudent @gender=,@recordcount=@rc output print @rc --结果为男的条数 -----------------3、选择性别男,年龄>15的学生-------------------------- create proc proc_SelecteStudent @gender char(2), --参数类型要与表tb_Student中列stGender一致,参数名随意 @age int --参数类型要与表tb_Student中列stAge一致,参数名随意 as begin --表tb_Student中有列stAge、stGender select * from tb_Student where stAge>=@age and stGender=@gender end --调用执行: exec proc_SelecteStudent @gender=,@age=15 --选择性别男,年龄>15的学生 -----------------4、分页查询,使用存储过程proc_SelectStudentByPage--------------------- set nocount off --不显示结果 select * form tb_Student create proc proc_SelectStudentByPage @pagesize int=7, --每页记录条数,默认7 @pageindex int=1, --当前查看第几页,默认1 @recordcount int output, --总条数,输出参数 @pagecount int output --总页数,输出参数 as begin --1、查询: select --只查询id、age t.id, t.age form (select *, rn=row_number() over (order by id asc) from tb_Student) as t where t.rn between (@pageindex-1)*@pagesize+1 and @pagesize*@pageindex --2、计算总条数: set @recordcount=(select count(*) from tb_Student) --3、计算总页数: set @pagecount=ceilling(@recordcount*1.0/@pagesize) --recordcount*1.0可变为小数,ceilling(天花板)向上取整 end --调用执行: select * form tb_Student declare @rc int,@pc int --声明rc、pc,存储两个输出参数 exec proc_SelectStudentByPage @recordcount=@rc output,@pagecount=@pc output --得到第1页的结果(显示7条) --exec proc_SelectStudentByPage @pagesize=7,@pageindex=2, @recordcount=@rc output,@pagecount=@pc output --得到第2页的结果(显示7条) print @rc --条数 Print @pc --页数

 

SQL Server存储过程

标签:alert   over   tag   向上取整   类型   div   过程   查询   cts   

人气教程排行