mysql subindex_mssql server sql分页存储过程
set quoted_identifier on
go
set ansi_nulls on
go
create proc execbypage
@sqlquery varchar(2000), --//輸入參數:sql檢索語句或表名
@pagesize int, --//輸入參數:每頁顯示記錄條數
@pageindex int --//輸入參數:當前頁碼
as
set nocount on
set ansi_warnings off
declare @tmptablename varchar(50)
set @tmptablename = '##tb1516_' + replace(cast(newid() as varchar(40)),'-','') --//生成隨機臨時表名稱
declare @subindex int
set @subindex = charindex('from',@sqlquery)
if (@subindex > 0)
begin --//帶from的標準檢索語句
declare @sqlquery1 varchar(2000)
declare @sqlquery2 varchar(2000)
set @sqlquery1 = substring(@sqlquery,1,@subindex - 1)
set @sqlquery2 = substring(@sqlquery,@subindex,len(@sqlquery))
set @sqlquery = @sqlquery1 + ',identity(numeric,1,1) as id1516 into ' + @tmptablename + ' ' + @sqlquery2
end
else --//不帶from的表名
begin
set @sqlquery = 'select *,identity(numeric,1,1) as id1516 into ' + @tmptablename + ' from' + @sqlquery
end
exec(@sqlquery) --//建立并初始化臨時表數據
declare @indexstart varchar(20),@indexend varchar(20)
set @indexstart = cast((@pageindex-1)*@pagesize+1 as varchar(20)) --//數據起始行id
set @indexend = cast(@pageindex * @pagesize as varchar(20)) --//數據結束行id
exec('select * from ' + @tmptablename + ' where id1516 between ' + @indexstart + ' and ' + @indexend) --//檢索該頁數據
exec('select max(id1516) as recordcount from ' + @tmptablename) --//提取總條數
exec('drop table ' + @tmptablename) --//刪除臨時表
go
set quoted_identifier off
go
set ansi_nulls on
go
總結
以上是生活随笔為你收集整理的mysql subindex_mssql server sql分页存储过程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 64. 最小路径和(D
- 下一篇: java 画笔跟swing组件_Java