2014-02-12 98 views
0

我寫了這個查詢得到的數據爲特殊的關鍵字:如何在INNER JOIN中使用SQL ROW_NUMBER?

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 

我想用這篇文章自定義分頁:Custom Paging using SQL Server Stored Procedure

我寫此查詢,但我得到一個錯誤:

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 

錯誤:

Msg 207, Level 16, State 1, Procedure GetAllSpecialPaperTags, Line 11
Invalid column name 'Papers.PID'.

爲什麼?

回答

3

這是您的order by表達:

 ORDER BY [Papers.PID] ASC 

它正在尋找將其全部 「Papers.PID」 命名列。它是而不是尋找Papers中的PID列。只需放下大括號:

 ORDER BY Papers.PID ASC 
+1

或[論文]。[PID]。 –