简体   繁体   English

如何在INNER JOIN中使用SQL ROW_NUMBER?

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

I have written this query to get data for special keyword: 我已编写此查询来获取特殊关键字的数据:

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

I want use this article for custom paging : Custom Paging using SQL Server Stored Procedure 我想将本文用于自定义分页: 使用SQL Server存储过程的自定义分页

I wrote this query but I'm getting an error: 我写了这个查询,但出现错误:

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

Error: 错误:

Msg 207, Level 16, State 1, Procedure GetAllSpecialPaperTags, Line 11 消息207,级别16,状态1,过程GetAllSpecialPaperTags,第11行
Invalid column name 'Papers.PID'. 无效的列名“ Papers.PID”。

Why? 为什么?

This is your order by expression: 这是您order by表达方式order by

        ORDER BY [Papers.PID] ASC

It is looking for a column named in its entirety "Papers.PID". 它正在寻找一个整体命名为“ Papers.PID”的列。 It is not looking for the PID column in Papers . 不在 Papers寻找PID列。 Just drop the braces: 只需放下括号:

        ORDER BY Papers.PID ASC

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

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