Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of rows in Oracle SQL Select?

I need to know how many records were returned in a select in oracle. Currently, I do two queries:

SELECT COUNT(ITEM_ID) FROM MY_ITEMS;

SELECT * FROM MY_ITEMS;

I need to know the COUNT but I hate doing two queries. Is there a way to do:

SELECT * FROM MY_ITEMS 

and then find out how many records are in there?

like image 314
twelshesgi Avatar asked May 21 '10 17:05

twelshesgi


2 Answers

Is there a way to do:

SELECT * FROM MY_ITEMS 

and then find out how many records are in there?

If you want it to be in this exact order, you can fetch all records on the client and count their number (almost all client libraries provide a function for that).

You can also do:

SELECT  i.*, COUNT(*) OVER ()
FROM    my_items i

, which will return you the count along with each record.

like image 61
Quassnoi Avatar answered Oct 17 '22 05:10

Quassnoi


If you're working in PL/SQL, you can use the SQL%ROWCOUNT pseudo-variable to get the number of rows affected by the last SQL statement. Might save you some effort.

like image 21
Jim Hudson Avatar answered Oct 17 '22 07:10

Jim Hudson