繁体   English   中英

如何在INNER JOIN中使用SQL ROW_NUMBER?

[英]How to use SQL ROW_NUMBER with INNER JOIN?

我已编写此查询来获取特殊关键字的数据:

ALTER procedure [dbo].[GetAllSpecialPaperTags]
    @PKeyword nvarchar(200)
as
begin
   select 
      Papers.PID, Papers.PTitle, Papers.PaperSummary 
  from 
      PaperKeywords 
  left join 
      PaperTags on PaperKeywords.PKeyID = PaperTags.PKeyID
  left join 
      Papers on PaperTags.PID = Papers.PID 
  where 
      PaperKeywords.PKeyword = @PKeyword
end

我想将本文用于自定义分页: 使用SQL Server存储过程的自定义分页

我写了这个查询,但出现错误:

create procedure [dbo].[GetAllSpecialPaperTags]
   @PageIndex INT = 1
   ,@PageSize INT = 10
   ,@RecordCount INT OUTPUT
   ,@PKeyword nvarchar(200)
as
BEGIN
      SET NOCOUNT ON;
      SELECT ROW_NUMBER() OVER
      (
            ORDER BY [Papers.PID] ASC
      )AS RowNumber
    ,Papers.PID , Papers.PTitle , Papers.PaperSummary
     INTO #Results
      from PaperKeywords 
        left join PaperTags on PaperKeywords.PKeyID = PaperTags.PKeyID
        left join Papers on PaperTags.PID = Papers.PID where PaperKeywords.PKeyword = @PKeyword

      SELECT @RecordCount = COUNT(*)
      FROM #Results

      SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

      DROP TABLE #Results
end

错误:

消息207,级别16,状态1,过程GetAllSpecialPaperTags,第11行
无效的列名“ Papers.PID”。

为什么?

这是您order by表达方式order by

        ORDER BY [Papers.PID] ASC

它正在寻找一个整体命名为“ Papers.PID”的列。 不在 Papers寻找PID列。 只需放下括号:

        ORDER BY Papers.PID ASC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM