通用分頁儲存過程,乾貨無汙染

2022-01-09 22:53:21 字數 1874 閱讀 6786

---判斷是否存在該儲存過程

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[web_pageview]') and objectproperty(id, n'isprocedure') = 1)

drop procedure [dbo].[web_pageview]

goset quoted_identifier on

goset ansi_nulls on

gocreate procedure dbo.web_pageview

@tablename nvarchar(200), -- 表名

@returnfields nvarchar(1000) = '*', -- 查詢列數

@pagesize int = 10, -- 每頁數目

@pageindex int = 1, -- 當前頁碼

@where nvarchar(1000) = '', -- 查詢條件

@orderby nvarchar(1000), -- 排序字段

@pagecount int output, -- 頁碼總數

@recordcount int output -- 記錄總數

as--設定屬性

set nocount on

-- 變數定義

declare @totalrecord int

declare @totalpage int

declare @currentpagesize int

declare @totalrecordforpageindex int

begin

if @where is null set @where=n''

-- 記錄總數

declare @countsql nvarchar(4000)

if @recordcount is null

begin

set @countsql='select @totalrecord=count(*) from '+@tablename+' '+@where

execute sp_executesql @countsql,n'@totalrecord int out',@totalrecord out

endelse

begin

set @totalrecord=@recordcount

end

set @recordcount=@totalrecord

set @totalpage=(@totalrecord-1)/@pagesize+1

set @currentpagesize=(@pageindex-1)*@pagesize

-- 返回總頁數和總記錄數

set @pagecount=@totalpage

set @recordcount=@totalrecord

-- 返回記錄

set @totalrecordforpageindex=@pageindex*@pagesize

exec ('select *

from (select top '+@totalrecordforpageindex+' '+@returnfields+', row_number() over ('+@orderby+') as pageview_rowno

from '+@tablename+ ' ' + @where +' ) as temppageviewtable

where temppageviewtable.pageview_rowno >

'+@currentpagesize)

endreturn 0

go

通用儲存過程 分頁儲存過程

名稱 spall returnrows 輸入 輸出 呼叫 exec spall returnrows select from 表名 頁號,返回記錄數,主鍵 排序字段 spall returnrows select from all categories 2,10,id id 說明 百萬級 通用儲存過...

通用分頁儲存過程

create procedure sp page strtable varchar 50 表名 strcolumn varchar 50 按該列來進行分頁 intcoltype int,strcolumn列的型別,0 數字型別,1 字元型別,2 日期時間型別 intorder bit,排序,0 順序...

通用分頁儲存過程

set quoted identifier on goset ansi nulls off go declare p1 int set p1 null exec sp pageview tbname n v question list fieldkey n id pagecurrent 3,page...