CREATE proc up_GetTopicList 
       @a_TableList Varchar(200),
       @a_TableName Varchar(30), 
       @a_SelectWhere Varchar(500),
       @a_SelectOrderId Varchar(20),
       @a_SelectOrder Varchar(50),
       @a_intPageNo int,
       @a_intPageSize int,
       @RecordCount int OUTPUT
as
   /*定义局部变量*/
   declare @intBeginID         int
   declare @intEndID           int
   declare @intRootRecordCount int
   declare @intRowCount        int
   declare @TmpSelect          NVarchar(600)
   /*关闭计数*/
   set nocount on
   
   /*求总共根贴数*/   select @TmpSelect = 'set nocount on;select @SPintRootRecordCount = count(*) from '+@a_TableName+' '+@a_SelectWhere
   execute sp_executesql 
             @TmpSelect,
             N'@SPintRootRecordCount int OUTPUT',
             @SPintRootRecordCount=@intRootRecordCount OUTPUTselect @RecordCount = @intRootRecordCount
 /*如果没有贴子,则返回零*/
   if (@intRootRecordCount = 0)  
       return 0
       
   /*判断页数是否正确*/
   if (@a_intPageNo - 1) * @a_intPageSize > @intRootRecordCount
      return (-1)   /*求开始rootID*/
   set @intRowCount = (@a_intPageNo - 1) * @a_intPageSize + 1
   /*限制条数*/   select @TmpSelect = 'set nocount on;set rowcount @SPintRowCount;select @SPintBeginID = '+@a_SelectOrderId+' from '+@a_TableName+' '+@a_SelectWhere+' '+@a_SelectOrder
   execute sp_executesql 
             @TmpSelect,
             N'@SPintRowCount int,@SPintBeginID int OUTPUT',
             @SPintRowCount=@intRowCount,@SPintBeginID=@intBeginID OUTPUT
   /*结束rootID*/
   set @intRowCount = @a_intPageNo * @a_intPageSize
   /*限制条数*/   select @TmpSelect = 'set nocount on;set rowcount @SPintRowCount;select @SPintEndID = '+@a_SelectOrderId+' from '+@a_TableName+' '+@a_SelectWhere+' '+@a_SelectOrder
   execute sp_executesql 
             @TmpSelect,
             N'@SPintRowCount int,@SPintEndID int OUTPUT',
             @SPintRowCount=@intRowCount,@SPintEndID=@intEndID OUTPUT
if @a_SelectWhere=' or @a_SelectWhere IS NULL
   select @TmpSelect = 'set nocount off;set rowcount 0;select '+@a_TableList+' from '+@a_TableName+' where '+@a_SelectOrderId+' between '
else
   select @TmpSelect = 'set nocount off;set rowcount 0;select '+@a_TableList+' from '+@a_TableName+' '+@a_SelectWhere+' and '+@a_SelectOrderId+' between 'if @intEndID > @intBeginID
   select @TmpSelect = @TmpSelect+'@SPintBeginID and @SPintEndID'+' '+@a_SelectOrder
else
   select @TmpSelect = @TmpSelect+'@SPintEndID and @SPintBeginID'+' '+@a_SelectOrder   execute sp_executesql 
             @TmpSelect,
             N'@SPintEndID int,@SPintBeginID int',
             @SPintEndID=@intEndID,@SPintBeginID=@intBeginID   return(@@rowcount)
   --select @@rowcount
GO
Server: Msg 170, Level 15, State 1, Procedure up_GetTopicList, Line 60
Line 60: Incorrect syntax near '+@a_TableName+'.
Server: Msg 170, Level 15, State 1, Procedure up_GetTopicList, Line 62
Line 62: Incorrect syntax near '+@a_TableName+'.
Server: Msg 105, Level 15, State 1, Procedure up_GetTopicList, Line 71
Unclosed quotation  before the character string ',
             @SPintEndID=@intEndID,@SPintBeginID=@intBeginID   return(@@rowcount)
   --select @@rowcount
'.

解决方案 »

  1.   

    就59行缺了一个单引号
    CREATE proc up_GetTopicList 
           @a_TableList Varchar(200),
           @a_TableName Varchar(30), 
           @a_SelectWhere Varchar(500),
           @a_SelectOrderId Varchar(20),
           @a_SelectOrder Varchar(50),
           @a_intPageNo int,
           @a_intPageSize int,
           @RecordCount int OUTPUT
    as
       /*定义局部变量*/
       declare @intBeginID         int
       declare @intEndID           int
       declare @intRootRecordCount int
       declare @intRowCount        int
       declare @TmpSelect          NVarchar(600)
       /*关闭计数*/
       set nocount on
       
       /*求总共根贴数*/   select @TmpSelect = 'set nocount on;select @SPintRootRecordCount = count(*) from '+@a_TableName+' '+@a_SelectWhere
       execute sp_executesql 
                 @TmpSelect,
                 N'@SPintRootRecordCount int OUTPUT',
                 @SPintRootRecordCount=@intRootRecordCount OUTPUTselect @RecordCount = @intRootRecordCount
     /*如果没有贴子,则返回零*/
       if (@intRootRecordCount = 0)  
           return 0
           
       /*判断页数是否正确*/
       if (@a_intPageNo - 1) * @a_intPageSize > @intRootRecordCount
          return (-1)   /*求开始rootID*/
       set @intRowCount = (@a_intPageNo - 1) * @a_intPageSize + 1
       /*限制条数*/   select @TmpSelect = 'set nocount on;set rowcount @SPintRowCount;select @SPintBeginID = '+@a_SelectOrderId+' from '+@a_TableName+' '+@a_SelectWhere+' '+@a_SelectOrder
       execute sp_executesql 
                 @TmpSelect,
                 N'@SPintRowCount int,@SPintBeginID int OUTPUT',
                 @SPintRowCount=@intRowCount,@SPintBeginID=@intBeginID OUTPUT
       /*结束rootID*/
       set @intRowCount = @a_intPageNo * @a_intPageSize
       /*限制条数*/   select @TmpSelect = 'set nocount on;set rowcount @SPintRowCount;select @SPintEndID = '+@a_SelectOrderId+' from '+@a_TableName+' '+@a_SelectWhere+' '+@a_SelectOrder
       execute sp_executesql 
                 @TmpSelect,
                 N'@SPintRowCount int,@SPintEndID int OUTPUT',
                 @SPintRowCount=@intRowCount,@SPintEndID=@intEndID OUTPUT
    if @a_SelectWhere='' or @a_SelectWhere IS NULL
       select @TmpSelect = 'set nocount off;set rowcount 0;select '+@a_TableList+' from '+@a_TableName+' where '+@a_SelectOrderId+' between '
    else
       select @TmpSelect = 'set nocount off;set rowcount 0;select '+@a_TableList+' from '+@a_TableName+' '+@a_SelectWhere+' and '+@a_SelectOrderId+' between 'if @intEndID > @intBeginID
       select @TmpSelect = @TmpSelect+'@SPintBeginID and @SPintEndID'+' '+@a_SelectOrder
    else
       select @TmpSelect = @TmpSelect+'@SPintEndID and @SPintBeginID'+' '+@a_SelectOrder   execute sp_executesql 
                 @TmpSelect,
                 N'@SPintEndID int,@SPintBeginID int',
                 @SPintEndID=@intEndID,@SPintBeginID=@intBeginID   return(@@rowcount)
       --select @@rowcount
    GO
      

  2.   

    這個存儲過程很慢。不知為什么
    CREATE PROCEDURE [GetCustomersDataPage] 
        @PageIndex INT,
        @PageSize INT,
        @RecordCount INT OUT,
        @PageCount INT OUT
    AS
    SELECT @RecordCount = COUNT(*) FROM   Customers
    SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize) 
    DECLARE @SQLSTR NVARCHAR(1000)
    IF @PageIndex = 0 OR @PageCount <= 1
        SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+
    ' CustomerID, CompanyName,Address,Phone FROM   Customers ORDER BY CustomerID DESC'
    ELSE IF @PageIndex = @PageCount - 1        
        SET @SQLSTR =N' SELECT * FROM ( SELECT TOP '+STR( @RecordCount - @PageSize * @PageIndex )+
    ' CustomerID, CompanyName,Address,Phone FROM   Customers ORDER BY CustomerID ASC ) TempTable ORDER BY CustomerID DESC'
    ELSE    
        SET @SQLSTR =N' SELECT TOP '+STR( @PageSize )+' * FROM ( SELECT TOP '+STR( @RecordCount - @PageSize * @PageIndex )+
    ' CustomerID, CompanyName,Address,Phone FROM   Customers ORDER BY CustomerID ASC ) TempTable ORDER BY CustomerID DESC'
    EXEC (@SQLSTR)
     
    GO
      

  3.   

    你这个做的是 分页的存储过程吧 
    怎么不用SQL2005的 函数 ROW_NUMBER   我自己测试过 速度还可以 ROW_NUMBER
    返回结果集分区内行的序列号,每个分区的第一行从 1 开始。语法:
    ROW_NUMBER ( )     OVER ( [ <partition_by_clause> ] <order_by_clause> )备注:
    ORDER BY 子句可确定在特定分区中为行分配唯一 ROW_NUMBER 的顺序。参数:
    <partition_by_clause>
    将 FROM 子句生成的结果集划入应用了 ROW_NUMBER 函数的分区。
    <order_by_clause>
    确定将 ROW_NUMBER 值分配给分区中的行的顺序。返回类型:
    bigint示例:
    以下示例将根据年初至今的销售额,返回 AdventureWorks 中销售人员的 ROW_NUMBER。 USE AdventureWorks
    GO
    SELECT c.FirstName, c.LastName, ROW_NUMBER() OVER(ORDER BY SalesYTD DESC) AS 'Row Number', s.SalesYTD, a.PostalCode
    FROM Sales.SalesPerson s JOIN Person.Contact c on s.SalesPersonID = c.ContactID
    JOIN Person.Address a ON a.AddressID = c.ContactID
    WHERE TerritoryID IS NOT NULL AND SalesYTD <> 0以下示例将返回行号为 50 到 60(含)的行,并以 OrderDate 排序。 USE AdventureWorks;
    GO
    WITH OrderedOrders AS
    (SELECT SalesOrderID, OrderDate,
    ROW_NUMBER() OVER (order by OrderDate)as RowNumber
    FROM Sales.SalesOrderHeader ) 
    SELECT * 
    FROM OrderedOrders 
    WHERE RowNumber between 50 and 60;