2017-04-06 76 views
1

我正在使用@skip和@take參數進行分頁。分頁後我需要總行數。我怎樣才能做到這一點,作爲表現的最佳方式?如何在應用分頁後獲取總行數

CREATE PROCEDURE Search 
    @Take  Int   = 10 
    @Skip  Int   = 0 
    @TotalRows Int Out 
AS 

    BEGIN 
    Select 
        RowNo, FirstName, LastName 
       From 
        (
         Select Row_Number()Over(Order By Id) As [RowNo], 
          FirstName, 
          LastName  
         From Users     
        ) tblTemp 
       Where RowNo > @Skip And RowNo <= @Take + @Skip 

       Set @TotalRows = ??? -- I need the number of total rows after applying paging. How can I do this? 
    END 
+0

請標記您的DBMS,例如MySQL,SQL-Server,Oracle –

+1

@SteveLovell,謝謝。我編輯了標籤。 –

回答

0

您可以使用@@ROWCOUNT

程序

CREATE PROCEDURE Search 
    @Take  Int   = 10, 
    @Skip  Int   = 0, 
    @TotalRows Int Out 
AS 
BEGIN 
    Select 
     RowNo, 
     FirstName, 
     LastName 
    From (
     Select 
      Row_Number()Over(Order By Id) As [RowNo], 
      FirstName, 
      LastName  
     From Users     
    ) tblTemp 
    Where RowNo > @Skip And RowNo <= @Take + @Skip 

    Set @TotalRows = @@ROWCOUNT 
END 

您可以撥打PROC等;

declare @total_rows int; 
exec search 10, 0, @total_rows output 
select @total_rows 
+0

我做了你之前說過的東西,總是@TotalRows是10,所以你的回答是不正確的。 –

+0

'@total_rows output'就是你在程序中使用變量的方式嗎?你是怎麼稱呼程序搜索>>的 – Praveen

相關問題