存储过程,必须声明标量变量问题
各位朋友
以下是我写的一个存储过程分页方法,能够编译通过,但是在使用时,老是提示“消息 137,级别 15,状态 1,第 1 行
必须声明标量变量 "@TotalCount "。”请看
CREATE PROCEDURE Pagination
@TotalCount INT OUTPUT,
@TotalPage INT OUTPUT,
@Table NVARCHAR(25), --将要查询的表名
@Column NVARCHAR(150),--将要查询的字段,可多列
@OrderColumn NVARCHAR(50), --排序字段
@CountColumn NVARCHAR(20), --取这个字段的最大值
@PageSize INT,--分页大小
@CurrentPage INT, --要查询的页
@OrderType NVARCHAR(4),--如何排序
@Condition NVARCHAR(200) --查询条件
AS
DECLARE @PageCount INT,
@strSql NVARCHAR(500),
@strCount NVARCHAR(100)
SET @PageCount=@PageSize*@CurrentPage
BEGIN
IF @Condition!= 'NO '
BEGIN
SET @strCount= 'SELECT @TotalCount=COUNT(*) FROM '+ @Table + ' WHERE '+@Condition
SET @TotalPage=CEILING(str(@TotalCount/@PageSize))
SET @strSql= 'SELECT TOP '+ str(@PageSize) + ' '+ @Column + ' FROM '+ @Table + '
WHERE ' + @CountColumn + '> (SELECT MAX( '+ @CountColumn + ')
FROM (SELECT TOP ' + str(@PageCount)+ ' '+ @CountColumn+ '
FROM ' + @Table + ' ORDER BY ' + @CountColumn + ') AS T) AND '+ @Condition + ' ORDER BY ' + @OrderColumn + ' '+ @OrderType
END
ELSE
BEGIN
SET @strCount= 'SELECT @TotalCount=COUNT(*) FROM '+ @Table
SET @TotalPage=CEILING(str(@TotalCount/@PageSize))
SET @strSql= 'SELECT TOP '+ str(@PageSize) + ' '+ @Column + ' FROM '+ @Table + '
WHERE ' + @CountColumn + '> (SELECT MAX( '+ @CountColumn + ')
FROM (SELECT TOP ' + str(@PageCount)+ ' '+ @CountColumn+ '
FROM ' + @Table + ' ORDER BY ' + @CountColumn + ') AS T)
ORDER BY ' + @OrderColumn + ' '+ @OrderType
END
EXEC (@strCount)
EXEC (@strSql)
END
下面是我在asp.net2.0中进行调用
public DataTable ShowBook(MShowBook mshowbook)
{
SqlParameter[] parms = new SqlParameter[10];
parms[0] = lzjbook.ParaInstance( "@Pagesize ", SqlDbType.Int, 4, mshowbook.Pagesize, ParameterDirection.Input);
parms[1] = lzjbook.ParaInstance( "@CurrentPage ", SqlDbTy