CREATE     PROCEDURE  [GetCustomersDataPage]    
                 @PageIndex  INT,           --以1开始
                 @PageSize    INT,  
                 @strUserName    VARCHAR(30),              
                 @RecordCount  INT  OUT,  
                 @PageCount  INT  OUT  
 
AS  
SELECT  @RecordCount=COUNT(*)  FROM MoneyRecord  WHERE UserName=@strUserName  
SET     @PageCount  = CEILING(@RecordCount  *  1.0  /  @PageSize)  
  
DECLARE  @SQLSTR  VARCHAR(500)    
       
if @PageIndex<=0 
   set @PageIndex=1
  
select * ,identity(int,1,1) AS SID INTO # FROM MoneyRecord  WHERE UserName=@strUserName
SET rowcount @pagesize
SET  @SQLSTR='select top '+convert(varchar,@PageSize)
             +' RecordID,myDateTime,MoneyType,MoneyNum,myKeyWords,Others FROM # where sid>'
             +convert(varchar,@PageSize*(@PageIndex-1))
             +' order by RecordID desc'
 
EXEC  (@SQLSTR)
set rowcount 0
drop table #GO

解决方案 »

  1.   

    --另外,能不能把这个存储过程中的strUserName改为strWhere(搜索条件)
    CREATE     PROCEDURE  [GetCustomersDataPage]    
                     @PageIndex  INT,           --以1开始
                     @PageSize    INT,  
                     @strUserName    VARCHAR(30),              
                     @RecordCount  INT  OUT,  
                     @PageCount  INT  OUT  
     
    AS  
    DECLARE  @SQLSTR  NVARCHAR(500)    set @sqlstr='SELECT  @RecordCount=COUNT(*)  FROM MoneyRecord  '+@strUserName  
    exec sp_executesql @sqlstr,N'@recordcount int output',@recordcount output
    SET     @PageCount  = CEILING(@RecordCount  *  1.0  /  @PageSize)  
             
    if @PageIndex<=0 
       set @PageIndex=1
      
    select * ,identity(int,1,1) AS SID INTO # FROM MoneyRecord  WHERE UserName=@strUserName
    SET rowcount @pagesize
    SET  @SQLSTR='select top '+convert(varchar,@PageSize)
                 +' RecordID,myDateTime,MoneyType,MoneyNum,myKeyWords,Others FROM # where sid>'
                 +convert(varchar,@PageSize*(@PageIndex-1))
                 +' order by RecordID desc'
     
    EXEC  (@SQLSTR)
    set rowcount 0
    drop table #GO
      

  2.   

    to: wgsasd311(自强不息)
    运行时提示错误:
    无法使用 SELECT INTO 语句向表 '#' 中添加标识列,该表中已有继承了标识属性的列 'RecordID'。
      

  3.   

    /*
    to: wgsasd311(自强不息)
    运行时提示错误:
    无法使用 SELECT INTO 语句向表 '#' 中添加标识列,该表中已有继承了标识属性的列 'RecordID'。
    */
    --另外,能不能把这个存储过程中的strUserName改为strWhere(搜索条件)
    CREATE     PROCEDURE  [GetCustomersDataPage]    
                     @PageIndex  INT,           --以1开始
                     @PageSize    INT,  
                     @strUserName    VARCHAR(30),              
                     @RecordCount  INT  OUT,  
                     @PageCount  INT  OUT  
     
    AS  
    DECLARE  @SQLSTR  NVARCHAR(500)    set @sqlstr='SELECT  @RecordCount=COUNT(*)  FROM MoneyRecord  '+@strUserName  
    exec sp_executesql @sqlstr,N'@recordcount int output',@recordcount output
    SET     @PageCount  = CEILING(@RecordCount  *  1.0  /  @PageSize)  
             
    if @PageIndex<=0 
       set @PageIndex=1
      
    select *,sid=(select count(1) from moneyrecord where username=@strusername and RecordID<=a.RecordID) into #
    FROM MoneyRecord a  WHERE UserName=@strUserName
    SET rowcount @pagesize
    SET  @SQLSTR='select top '+convert(varchar,@PageSize)
                 +' RecordID,myDateTime,MoneyType,MoneyNum,myKeyWords,Others FROM # where sid>'
                 +convert(varchar,@PageSize*(@PageIndex-1))
                 +' order by RecordID desc'
     
    EXEC  (@SQLSTR)
    set rowcount 0
    drop table #GO