一位高人给了下面的代码:
CREATE     PROCEDURE  [GetCustomersDataPage]    
                 @PageIndex  INT,           --以1开始
                 @PageSize    INT,  
                 @strwhere    VARCHAR(100),              
                 @RecordCount  INT  OUT,  
                 @PageCount  INT  OUT  
 
AS  
DECLARE  @SQLSTR  NVARCHAR(500)  
if ltrim(@strwhere)=''
set @strwhere='1=1'set @SQLSTR='SELECT  @RecordCount=COUNT(*)  FROM MoneyRecord  WHERE '+@strwhere
exec sp_executesql @sqlstr,'@recordcount int output',@recordcount output
SET     @PageCount  = CEILING(@RecordCount  *  1.0  /  @PageSize)  
         
if @PageIndex<=0 
   set @PageIndex=1
  
SET  @SQLSTR='select top '+convert(varchar,@PageSize)
             +' RecordID,myDateTime,MoneyType,MoneyNum,myKeyword,Others FROM [MoneyRecord] t'
             +' where (select count(1) from [MoneyRecord] where RecordID>t.RecordID and UserID=t.UserID)+1>'
             +convert(varchar,@PageSize*(@PageIndex-1))
             +' and '+@strwhere
             +' order by RecordID desc'
 
EXEC  (@SQLSTR)
GO
------------------我调试的时候输入参数PageIndex =1 PageSize=5 strwhere="" 
程序进行到exec sp_executesql @sqlstr,'@recordcount int output',@recordcount output时报错:过程需要参数 '@parameters' 为 'ntext/nchar/nvarchar' 类型。exec sp_executesql @sqlstr,'@recordcount int output',@recordcount output
就是这句话有问题,其它都好

解决方案 »

  1.   

    --try,没有把"where"包括进去。
    ALTER     PROCEDURE  [GetCustomersDataPage]    
                     @PageIndex  INT,           --以1开始
                     @PageSize    INT,  
                     @strWhere    VARCHAR(100),              
                     @RecordCount  INT  OUT,  
                     @PageCount  INT  OUT  
     
    AS  
    declare @sql nvarchar(4000) 
    set @sql=N'select @RecordCount=count(*) FROM MoneyRecord where '+@strWhere+' '
    exec sp_executesql @sql,N'@RecordCount int output',@RecordCount output
    SET     @PageCount  = CEILING(@RecordCount  *  1.0  /  @PageSize)  
      
    DECLARE  @SQLSTR  VARCHAR(500)    
           
    if @PageIndex<=0 
       set @PageIndex=1
      
    SET  @SQLSTR='select top '+convert(varchar,@PageSize)
                 +' RecordID,myDateTime,MoneyType,MoneyNum,myKeyword,Others FROM [MoneyRecord] t'
                 +' where '+@strWhere+' and (select count(1) from [MoneyRecord] where RecordID>t.RecordID and UserID=t.UserID)+1>'
                 +convert(varchar,@PageSize*(@PageIndex-1))
                 +' '
                 +' order by RecordID desc'EXEC  (@SQLSTR)
    GO
      

  2.   

    再定义一个变量啊,是字段变量@strcol varchar(100)
    select @sql='select 语句'
    exec(@sql)
    是可以实现的
      

  3.   

    确实运行起来了
    谢谢楼上两位再加一个小功能
    再传出一个参数 totalnum,把表中满足@strwhere条件的条目中的MoneyNum求和返回
    有一点:moneytype=1 表示收入,moneytype=0表示支出
    这个求和需要求差值,即 totalnum=总收入-总支出
      

  4.   

    还有一个问题:"UserID = 4 AND MoneyType=1 AND myDateTime> '2005-11-18' AND myDateTime< '2005-11-21'"这个句子作为strwhere输入执行的时候提示
    服务器: 消息 170,级别 15,状态 1,行 1
    [Microsoft][ODBC SQL Server Driver][SQL Server]第 1 行: 'UserID = 4 AND MoneyType=1 AND myDateTime> '2005-11-18' AND myDateTime< '2005-11-21'' 附近有语法错误。
    获取调用堆栈失败!怎么回事啊
      

  5.   

    --try,没有把"where"包括进去。
    ALTER     PROCEDURE  [GetCustomersDataPage]    
                     @PageIndex  INT,           --以1开始
                     @PageSize    INT,  
                     @strWhere    VARCHAR(2000),              
                     @RecordCount  INT  OUT,  
                     @PageCount  INT  OUT  
     
    AS  
    declare @sql nvarchar(4000) 
    set @sql=N'select @RecordCount=count(*) FROM MoneyRecord where '+@strWhere+' '
    exec sp_executesql @sql,N'@RecordCount int output',@RecordCount output
    SET     @PageCount  = CEILING(@RecordCount  *  1.0  /  @PageSize)  
      
    DECLARE  @SQLSTR  VARCHAR(500)    
           
    if @PageIndex<=0 
       set @PageIndex=1
      
    SET  @SQLSTR='select top '+convert(varchar,@PageSize)
                 +' RecordID,myDateTime,MoneyType,MoneyNum,myKeyword,Others FROM [MoneyRecord] t'
                 +' where '+@strWhere+' and (select count(1) from [MoneyRecord] where RecordID>t.RecordID and UserID=t.UserID)+1>'
                 +convert(varchar,@PageSize*(@PageIndex-1))
                 +' '
                 +' order by RecordID desc'EXEC  (@SQLSTR)
    GO
      

  6.   

    谢谢zlp321002(茫茫人海一次擦肩,魂系梦牵度日如年,相见恨晚心有何怨)
    你的这个可以运行
    可以不可以再帮我看看下面的问题:1、再加一个小功能
    再传出一个参数 totalnum,把表中满足@strwhere条件的条目中的MoneyNum求和返回
    有一点:moneytype=1 表示收入,moneytype=0表示支出
    这个求和需要求差值,即 totalnum=总收入-总支出
    还有一个问题:"UserID = 4 AND MoneyType=1 AND myDateTime> '2005-11-18' AND myDateTime< '2005-11-21'"这个句子作为strwhere输入执行的时候提示
    服务器: 消息 170,级别 15,状态 1,行 1
    [Microsoft][ODBC SQL Server Driver][SQL Server]第 1 行: 'UserID = 4 AND MoneyType=1 AND myDateTime> '2005-11-18' AND myDateTime< '2005-11-21'' 附近有语法错误。
    获取调用堆栈失败!
      

  7.   

    --tryALTER     PROCEDURE  [GetCustomersDataPage]    
                     @PageIndex  INT,           --以1开始
                     @PageSize    INT,  
                     @strWhere    VARCHAR(2000),              
                     @RecordCount  INT  OUT,  
                     @PageCount  INT  OUT,  
       @totalnum  int out
    AS  
    declare @sql nvarchar(4000) 
    set @sql=N'select @RecordCount=count(*) FROM MoneyRecord where '+@strWhere+' '
    exec sp_executesql @sql,N'@RecordCount int output',@RecordCount output
    SET     @PageCount  = CEILING(@RecordCount  *  1.0  /  @PageSize)  
      
    DECLARE  @SQLSTR  VARCHAR(4000)    
           
    if @PageIndex<=0 
       set @PageIndex=1
      
    SET  @SQLSTR='select top '+convert(varchar,@PageSize)
                 +' RecordID,myDateTime,MoneyType,MoneyNum,myKeyword,Others FROM [MoneyRecord] t'
                 +' where '+@strWhere+' and (select count(1) from [MoneyRecord] where RecordID>t.RecordID and UserID=t.UserID)+1>'
                 +convert(varchar,@PageSize*(@PageIndex-1))
                 +' '
                 +' order by RecordID desc'EXEC  (@SQLSTR)declare @sql2 nvarchar(4000)
    set @sql=N'select top '+convert(varchar,@PageSize)
                 +' @totalnum=sum(case when MoneyType=1 then  MoneyNum else -MoneyNum end) FROM [MoneyRecord] t'
                 +' where '+@strWhere+' and (select count(1) from [MoneyRecord] where RecordID>t.RecordID and UserID=t.UserID)+1>'
                 +convert(varchar,@PageSize*(@PageIndex-1))
                 +' '
                 +' order by RecordID desc'
    exec sp_executesql @sql,N'@totalnum int output',@totalnum output
    GO
      

  8.   

    列名 't.RecordID' 在 ORDER BY 子句中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
    -------------------------
    虽然没有解决,但先给分吧
    我另外开辟新帖子问大家