sqlserver分頁儲存過程

2022-07-22 04:30:13 字數 2551 閱讀 1388

sqlserver 單錶(檢視)通用分頁儲存過程

create

procedure

dbo.proc_getpage

@table_name

varchar(500), --

表名,多表連線:tablea a left join tableb b on a.id=b.fk_id

@select_fields

varchar(1000)='*

', --

需要返回的列

@page_size

int=

10, --

頁尺寸,如果為0則表示返回所有行,不分頁

@page_index

int=

1, --

頁碼@sort_field

varchar(50)=

'', --

排序字段

@order_type

varchar(10)=

'desc

', --

排序型別, asc|desc

@str_where

varchar(max)=

'', --

查詢條件(注意: 不要加 where)

@row_count

int output --

總記錄數

asbegin

declare

@select_sql

varchar(max) --

主語句declare

@count_sql

nvarchar(max) --

獲取總記錄數語句

declare

@order_sql

varchar(400) --

排序子語句

if(@str_where

!=''

)

begin

set@count_sql

=n'select @rowcount=count(*) from '+

@table_name

+ n'

where '+

@str_where

endelse

begin

set@count_sql

=n'select @rowcount=count(*) from '+

@table_name

endset

@order_sql='

order by '+

@sort_field+'

'+@order_type

if(@page_size=0

)

begin

if(@str_where

!=''

)

begin

set@select_sql='

select '+

@select_fields+'

from '+

@table_name+'

where '+

@str_where+'

'+@order_sql

endelse

begin

set@select_sql='

select '+

@select_fields+'

from '+

@table_name+'

'+@order_sql

endend

else

begin

set@select_sql='

select * from (select row_number() over('+

@order_sql+'

) as row_no,'+

@select_fields+'

from '+

@table_name

if(@str_where

!=''

)

begin

set@select_sql+='

where '+

@str_where

endset

@select_sql+='

) as temp where row_no between '+

cast(((@page_index

-1)*

@page_size

+1) as

varchar) +

'and '+

cast((@page_index

*@page_size) as

varchar

)

endexec sp_executesql @count_sql,n'

@rowcount int output

',@row_count

output

exec(@select_sql

)end

SQL SERVER分頁儲存過程

使用儲存過程寫乙個分頁查詢 select from t users gocreate proc usp getdatafy pagesize int 10,每頁記錄條數 定義變數並賦初始值 pageindex int 1,當前要檢視第幾頁的記錄 sumcount int output,總的記錄條數 ...

SqlServer分頁儲存過程

高效分頁語句 1 row number select from select row number over order byrpid as rowfrom ou rolepermission as twheret.row 0 andt.row 10 2,top 分頁查詢 select top 10...

sql server 分頁儲存過程

sql server 分頁儲存過程,在網上找了很多,但是都不能用或者不能很好的用,特別綜合大家的智慧型,自己寫了乙個,而且通過visual studio除錯,通過實際資料測試 set ansi nulls on set quoted identifier on go alter procedure ...