How to select data and the total to cursors

oracleselect

I have a table like this:

 ID  | Name
------------
  1  | AA
  2  | AA
  3  | AA
  4  | BB

I want to select all rows where Name like 'AA' in a cursor and get the count from this result in another cursor. I also need to page the results.

Expected result (Skip = 0; Take = 2)

-- p_result

 ID  | Name
------------
  1  | AA
  2  | AA

-- p_count
   3

How can I do this?

Best Answer

An alternative to msi77's solution is the use of analytical functions. This might be a bit more efficient but you should run an explain plan to find out.

SELECT id,
       name, 
       count(*) over () as total_count
FROM your_table
WHERE name = 'AA'