CREATE procedure GetProductsInCategoryintCount
              ( @CategoryID Int,
@pagesize int,
             @pageindex int,
             @docount bit)
             as
             set nocount on
             if(@docount=1)
             select count(ProductID) from Product INNER JOIN ProductCategory ON Product.ProductID = ProductCategory.ProductID where productcategory.categoryid = @categoryid
             else
             begin
             declare @indextable table(id int identity(1,1),nid int)
             declare @PageLowerBound int
             declare @PageUpperBound int
             set @PageLowerBound=(@pageindex-1)*@pagesize
             set @PageUpperBound=@PageLowerBound+@pagesize
             set rowcount @PageUpperBound
             insert into @indextable(nid) select ProductID from Product INNER JOIN ProductCategory ON Product.ProductID = ProductCategory.ProductID where productcategory.categoryid = @categoryid order by ProductID desc
             select O.* from Product INNER JOIN ProductCategory ON Product.ProductID = ProductCategory.ProductID O,@indextable t where O.ProductID=t.nid
             and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id
             end
             set nocount off
            GO

解决方案 »

  1.   

    select O.* from Product INNER JOIN ProductCategory ON Product.ProductID = ProductCategory.ProductID O,@indextable t where O.ProductID=t.nid
                 and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id-- 改为
    select O.* 
    from Product
    INNER JOIN ProductCategory O
    ON Product.ProductID = ProductCategory.ProductID
    INNER JOIN @indextable t 
    ON O.ProductID=t.nid
    WHERE t.id>@PageLowerBound and t.id<=@PageUpperBound 
    order by t.id
      

  2.   

    别名应该跟在表名后, 而不是on 条件后面
    JOIN方式要么统一使用WHERE的形式, 要么用JOIN的方式, 不能混用.
      

  3.   

    错误209;列名"productid"不明确
    列名"productid"不明确
    列前缀Productcategory与查询中所用的表名或别名不配