存储过程:

-- =============================================
-- Author:
-- Create date: 
-- Description:    分页
--Update Date:
--增加了默认排序规则,根据主键升序(防止在视图查询中乱号)
-- =============================================
ALTER PROCEDURE [dbo].[getdatabyPageIndex]
@tablename nvarchar(200),
@columns nvarchar(500)='*',
@condition nvarchar(200)='',
@pagesize int=10,
@pageindex int=0,
@pk nvarchar(30),
@total int output, --统计总共的条数
@orderculumn nvarchar(50)=@pk,
@isasc nvarchar(10)='desc'

AS
BEGIN
    DECLARE @sql nvarchar(2000)
    SET @sql='select top '+cast(@pagesize AS nvarchar(10))+' '+@columns+' from '+@tablename+' where '+
    @pk+' not in (select top '+cast((@pagesize*@pageindex) AS nvarchar(10))+
    ' '+@pk+' from '+@tablename +' where 1=1 '+@condition+' order by '+@orderculumn+' '+@isasc+')'+@condition +' order by '+@orderculumn+' '+@isasc
     PRINT @sql
        EXEC(@sql)
        DECLARE @sql2 nvarchar(2000)        
        SET  @sql2='SELECT @total1 = count(*) FROM '+ @tablename+' WHERE 1=1 '+ @condition
        EXEC sp_executesql @sql2,N'@total1 int output',@total output
       
END
View Code

相关文章:

  • 2022-02-24
  • 2022-12-23
  • 2021-07-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-17
  • 2022-01-05
  • 2021-10-18
  • 2022-03-10
相关资源
相似解决方案