sqlsql-server-2008pagination

How to execute two commands at the same time in SQL Server 2008


How I can execute two commands at the same time in SQL Server?

I want to get a row count of a table and some row according to the row count. For example I want to get row count of a table and assuming I want to get last 50 rows on a single page.

How can I do this?


EDIT 1):

Consider that I first get count and it return to me 100 and I want to get 80 to 100 recordsin this point another tansaction may delete 70 to 100 records and I can not get appropriate records


Solution

  • An inline count my be evaluated many times or may give different results. A separate CROSS JOIN approach will give different results at some point

    See for why with repro script: can I get count() and rows from one sql query in sql server?

    ;WITH aCTE AS
    (
        SELECT
           *,
           COUNT(*) OVER () AS TotalRows,
           ROW_NUMBER OVER (ORDER BY SomeLatest DESC) AS rn
        FROM
           MyTable
    ) foo
    SELECT
       *
    FROM
       foo
    WHERE
       rn <= 50