0

我想为分页[主要动机]创建动态查询,这是我的代码:

Alter proc proc_GetData
(
    @TableName varchar(500)='tblPropertyType',
    @PrimaryKey varchar(500)='Id',
    @Columns varchar(max)='PropertyType',
    @WhereCondition varchar(max)='',
    @RecsPerPage int =3,
    @Page int=1,
    @SortColumn varchar(500)='Id',
    @SortDirection Varchar(56)='asc'
)
as
DECLARE @SQL VARCHAR(MAX)
DECLARE @DSQL VARCHAR(MAX)
DECLARE @FirstRec int, @LastRec int
SET @FirstRec = (@Page - 1) * @RecsPerPage
SET @LastRec = (@Page * @RecsPerPage + 1)
select @FirstRec
select @LastRec
SET @SQL='
SELECT
ROW_NUMBER() OVER (ORDER BY ' + @SortColumn+ ' '+ @SortDirection +') RowNum,'+ @PrimaryKey +' , ' +@Columns + ' from ' +@TableName+ ' Where  1=1 '+
@WhereCondition

我想做的是:

  • 首先:从上述查询中惰性化临时表中的所有记录。
  • 第二:SELECT * FROM @TEMPResult WHERE RowNum > @FirstRec AND RowNum < @LastRec

请帮我

4

1 回答 1

-6
SET @SQL='SELECT ROW_NUMBER() OVER 
   (ORDER BY ' + @SortColumn+ ' ' + @SortDirection + ') RowNum, ' + @PrimaryKey + ', ' + @Columns + ' 
   INTO tempTableName
   FROM ' + @TableName + ' Where  1 = 1 ' + @WhereCondition
于 2013-03-25T11:48:41.580 回答