0
CREATE PROCEDURE [dbo].[sp_GetPageWiseData]
(
    @tableName sysname,
    @orderColumn nvarchar(100),
    @PageIndex INT = 1,
    @PageSize INT = 10,
    @RecordCount varchar(10) OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @query varchar(2000),
        @minimumIndex varchar(5),
        @maximumIndex varchar(5)

SET @minimumIndex=convert(varchar,(@PageIndex - 1) * @PageSize + 1)
SET @maximumIndex=convert(varchar,@PageIndex * @PageSize)

SET @query='SELECT ROW_NUMBER() OVER(ORDER BY ' + @orderColumn + ' ASC)AS RowNumber,* INTO #Results FROM ' + @tableName + ';
            SELECT ' + @RecordCount + '=COUNT(*) FROM #Results;
            SELECT * FROM #Results WHERE RowNumber BETWEEN ' + @minimumIndex + ' AND ' + @maximumIndex + ';
            DROP TABLE #Results'
Exec (@query)   
END

这里的问题是当程序执行时,输出参数@RecordCount显示 NULL 值。

为什么?

请解释。谢谢

4

2 回答 2

0

Your query must be like this:

SET @query='SELECT ROW_NUMBER() OVER(ORDER BY ' + @orderColumn + ' ASC)AS RowNumber,* INTO #Results FROM ' + @tableName + ';
        SELECT  @RecordCount =COUNT(*) FROM #Results;
        SELECT * FROM #Results WHERE RowNumber BETWEEN ' + @minimumIndex + ' AND ' + @maximumIndex + ';
        DROP TABLE #Results';

But here system will ask you to declare the variable, @RecordCount

so you can do it like this by returning 2 datasets from the query;

SET @query='DECLARE @RecordCount varchar(10); SELECT ROW_NUMBER() OVER(ORDER BY ' + @orderColumn + ' ASC)AS RowNumber,* INTO #Results FROM ' + @tableName + ';
        SELECT  @RecordCount =COUNT(*) FROM #Results;
        SELECT * FROM #Results WHERE RowNumber BETWEEN ' + @minimumIndex + ' AND ' + @maximumIndex + ';
        SELECT @RecordCount AS TOTALRECORDS;
        DROP TABLE #Results';
于 2012-06-05T06:14:44.853 回答
0

我认为您不能以这种方式使用它。这就像连接而不是值分配方法。我认为您应该分两步处理它,如下所示:

SET @query='SELECT ROW_NUMBER() OVER(ORDER BY ' + @orderColumn + ' ASC)AS RowNumber,* INTO #Results FROM ' + @tableName + ';'

Exec (@query)

SELECT @RecordCount =COUNT(*) FROM #Results

SET @query='SELECT * FROM #Results WHERE RowNumber BETWEEN ' + @minimumIndex + ' AND ' + @maximumIndex + ';
DROP TABLE #Results'

Exec (@query)
于 2012-06-05T06:24:22.890 回答