SQL真分頁儲存過程

2022-09-07 19:51:15 字數 1860 閱讀 1897

create proc [dbo].[proc_delete]

@tablename varchar(50),

@id varchar(5000)

asbegin

declare @strsql varchar(5000)

declare @sql varchar(4000)

set @sql='select col='''+ replace(@id,',',''' union all select ''')+''''

if object_id('tempdb..#delid') is not null

drop table #delid

create table #delid(id varchar(36))

insert into #delid exec(@sql)

set @strsql = 'delete from '+@tablename+' where id in ( select id from #delid)'

exec(@strsql)

endgo

create proc [dbo].[p_paging]

@tablename varchar(8000), --表名、檢視名

@indexcol varchar(50) = 'id', --標識列名(如:比如主鍵、標識,推薦使用索引列)

@pagesize int = 10, --頁面大小

@pageindex int = 0, --當前頁

@ordercol varchar(100) = 'id desc',--排序 (如:id)

@where varchar(max) = '', --條件

@columns varchar(500) = '*' --要顯示的列

asdeclare @sql varchar(max)

declare @sql2 varchar(max)

declare @where2 varchar(max)

if @where <> ''

begin

select @where2 = ' and ' + @where

select @where = ' where ' + @where

endelse

select @where2 = ''

select @sql = 'select top ' + convert(varchar(10),@pagesize) + ' ' + @columns + ' from ' + @tablename

select @sql2 = @sql + @where

select @sql = @sql + ' where ' + '(' + @indexcol + ' not in (select top ' + convert(varchar(10), @pagesize * @pageindex) + ' ' + @indexcol + ' from ' + @tablename + @where + ' order by '+ @ordercol +'))'

select @sql = @sql + @where2

select @sql = @sql + ' order by ' + @ordercol

--獲取資料集

exec (@sql)

print @sql

select @sql2 = replace(@sql2,'top ' + convert(varchar(10), @pagesize) + ' ' + @columns, 'count(1)')

--獲取總資料條數

exec(@sql2)

go

ROW NUMBER 真分頁 儲存過程

if exists select from sysobjects where name proc pages drop proc proc pages create proc proc pages tablename varchar 100 pagesize int,pageindex int,or...

sql 儲存過程分頁

create proc myx prpagerecordset querystr nvarchar 1000 keyfield nvarchar 200 pagesize int,pagenumber int as begin declare sqltext as nvarchar 4000 dec...

SQL 儲存過程 分頁

1.俄羅斯儲存過程 的改良版 create procedure pagination1 pagesize int,頁面大小,如每頁儲存20條記錄 pageindex int 當前頁碼 as set nocount on begin declare indextable table id int id...