亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

sql 2005 分頁存儲代碼

標簽:
MySQL SQL Server

不多说,直接上代码:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go






-- [SelectBase] 1,1,'Users','username=''test'''
ALTER procedure [dbo].[SelectBase]
@PageIndex                    int,
@PageSize                    int,
@TableName       nvarchar(max),
@Where                     nvarchar(max)=''

as
Declare @rowcount          int
Declare @intStart          int
Declare @intEnd         int

Declare @SQl nvarchar(max), @WhereR nvarchar(max), @OrderBy nvarchar(max)
set @rowcount=0
set nocount on

if @Where<>''
begin
  set @Where=' and '+@Where
end

if CHARINDEX('order by', @Where)>0
begin
    set @WhereR=substring(@Where, 1, CHARINDEX('order by',@Where)-1)    --取得条件
    set @OrderBy=substring(@Where, CHARINDEX('order by',@Where), Len(@Where))    --取得排序方式(order by 字段 方式)
end
else
begin
    set @WhereR=@Where
    declare @PKName nvarchar(50)
    if(len(@TableName)>50)
    begin
        Set @PKName='ID'
    end
    else
    begin
        select top 1 @PKName=[name] from syscolumns where id=object_id(@TableName) order by colstat desc
    end
    set @OrderBy=' order by '+@PKName+' asc'
end

set @SQl='SELECT @rowcount=count(*) from '+cast(@TableName as nvarchar(3000))+' where 1=1 '+@WhereR
exec sp_executeSql @SQl,N'@rowcount int output',@rowcount output

if @PageIndex=0 and @PageSize=0    --不进行分页,查询所有数据列表
begin
    set @SQl='SELECT * from '+cast(@TableName as nvarchar(3000))+' where 1=1 '+@Where
end
else    --进行分页查询数据列表
begin
    set @intStart=(@PageIndex-1)*@PageSize+1;
    set @intEnd=@intStart+@PageSize-1

    set @SQl='select * from(select *,ROW_NUMBER() OVER('+cast(@OrderBy as nvarchar)+') as row from '
    set @SQl=@SQL+@TableName+' where 1=1 '+@WhereR+') as a where row between '+cast(@intStart as varchar)+' and '+cast(@intEnd as varchar)
end
--print @SQl
exec sp_executeSql @SQl
--select @rowcount
return @rowcount

--------------------------------------------
--print @SQl
--exec [SelectBase] 1,8,'SpaceContent','UserInfoID=45'

set nocount off


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消