views:

79

answers:

3

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?

+6  A: 

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.

Quassnoi
If you want to do this at the database level, I agree with the second suggestion. You can see an example here: http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/functions032.htm#i82697
Intelekshual
+1, That's a clever way to do it! :)
FrustratedWithFormsDesigner
A: 

I'm just unsure about the table aliases, I don't remember in Oracle if they require 'AS' or not. But this should work.

select mt.*, c.Cntr
    from MyTable mt
        , (select COUNT(*) as Cntr
               from MyTable
           ) c
Will Marcouiller
Why the downvote? This will work.
Quassnoi
A: 

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.

Jim Hudson