Number of rows in Oracle SQL Select?

前端 未结 4 621
时光取名叫无心
时光取名叫无心 2021-02-08 02:18

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;
         


        
相关标签:
4条回答
  • 2021-02-08 02:38

    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.

    0 讨论(0)
  • 2021-02-08 02:49

    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.

    0 讨论(0)
  • 2021-02-08 03:00

    This ought to do the trick.

    WITH 
    base AS
    (
        SELECT *
        FROM MY_ITEMS
    )
    SELECT (SELECT COUNT(*) FROM base) kount,
           base.*
    FROM base
    
    0 讨论(0)
  • 2021-02-08 03:02

    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
    
    0 讨论(0)
提交回复
热议问题