SQL语句_记录

1、分页存储过程的创建和调用。

-------------创建分页存储过程——————————
if exists(select * from sysobjects where name='pading') --判断存储过程pading是否存在
drop procedure pading --存在则删除
go
create procedure pading		--创建存储过程pading
							--设置参数
	@currentPage int=0,		--当前页码,默认值为0,即第一页
	@pageSize int=5,		--一页显示多少条,默认值为5
	@countPage int output,	--输出参数,总共多少页
	@countRows int output	--输出参数,总共多少记录
as
select top (@pageSize) * from temp	--注意参数@pageSize需要用括号括起来
where id not in (select top (@pageSize*@currentPage) id from temp ) 
order by id
set @countRows=(select count(*) from temp)	--赋值。计算总共有多少条记录
set @countPage=@countRows/@pageSize	--赋值。计算总共有多少页
if @countRows%@pageSize<>0	--判断:如果总条数除以页显示条数不等于0
set @countPage=@countPage+1 --页码+1
---------------end------------------------------------------------------------------------

---------------执行分页存储过程-----------
declare @countpage int,@countrows int
exec pading 0,5,@countpage output,@countrows output
select @countpage,@countrows
---------------end------------------------
------------------------------创建分页存储过程(动态sql语句)------------------------------------
--发布时间:2011-08-26
--作者:qingyun1029
if exists (select * from sysobjects where name='splitpage')
drop procedure splitpage
go
create procedure splitpage	
	@pageSize int=10,
	@currentPage int =0,
	@countRecord int output,
	@countPage int output
as
declare @sql nvarchar(1000)
--注意:拼接sql语句时,字符串类型不能与数字类型用+号拼接,需要用cast转换,如下:
set @sql=N'select top '+cast(@pageSize as nvarchar(10))
		 +' * from temp where id not in (select top '
		 +cast((@pageSize*@currentPage) as nvarchar(10))+' id from temp)'
exec sp_executesql @sql
set @countRecord=(select count(*) from temp)
set @countPage=@countRecord/@pageSize
if(@countRecord%@pageSize<>0)
set @countPage=@countPage+1
------------------------------调用存储过程------------------------------------
declare @cRecord int, @cPage int
exec splitpage 500,2,@cRecord output,@cPage output
select @cRecord,@cPage
--------------------------------end-----------------------------------------------

2、sql中使用循环,插入测试数据

declare @star int
set @star=0
while @star<100000
begin
	insert into temp
	values('qingyun',24)
	set @star=@star+1
end


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值