Getting the count of records in a cursor

Hi,
How can I get the count of records satisfying a conditin in a cursor.
This is the code that I have written. Please tell me where I am going wrong.
declare
cursor c is
select * from Details where Name like 'M%';
rec number;
r c%rowtype;
begin
for r in c
loop
rec:=rec+1;
end loop;
dbms_output.put_line(rec);
end;
Thanks in advance

Hi,
You have to initialize the rec variable.
declare
cursor c is
select * from Details where Name like 'M%';
rec number :=0;
r c%rowtype;
begin
for r in c
loop
rec:=rec+1;
end loop;
dbms_output.put_line(rec);
end;
You can also use the %ROWCOUNT attribute.
Regards

Similar Messages

  • To get the count of records and able to access the column value in a single

    Hi
    Is there any way to get the number of records in the query and access the column values
    e.g
    select count(*)
    from
    (SELECT department, COUNT(*) as "Number of employees"
    FROM employees
    WHERE salary > 25000
    GROUP BY department ) a
    This wil only get the Count, if i want to access each row from the inline view how can i do that.

    Your question is not clear.
    Are you looking for total record count as well as count by department ?
    Something like this?
    SQL>
    SQL> with temp as
      2  (
      3  select 1 dept ,10000 sal from dual union
      4  select 1 dept ,25100 sal from dual union
      5  select 1 dept ,30000 sal from dual union
      6  select 1 dept ,40000 sal from dual union
      7  select 2 dept ,10000 sal from dual union
      8  select 2 dept ,25100 sal from dual union
      9  select 2 dept ,30000 sal from dual union
    10  select 2 dept ,40000 sal from dual )
    11  select count(*) over( partition by 1 ) total_count,dept,
    12  count(*) over(partition by dept) dept_cnt  from temp
    13  where sal>25000;
    TOTAL_COUNT       DEPT   DEPT_CNT
              6          1          3
              6          1          3
              6          1          3
              6          2          3
              6          2          3
              6          2          3
    6 rows selected
    SQL>

  • Select Count(*) from Sample_table - how to get the count using JDBC?

    Hi All,
    It would be glad if anyone could help me with this. The problem is that I have to get the 'count' of records selected from a arbitrary table say, 'sample_table'. Is that possible to form the SQL in JDBC as
    Select Count(*) from Sample_table
    and get the value of the count? If yes, how?
    Thanks in advance
    Prabz

    stmt = con.createStatement();
    ResultSet recordcnt_rs = stmt.executeQuery("Select Count (*) as record_ctr From Sample_table");
    recordcnt_rs.next();     
    record_ctr = recordcnt_rs.getInt("record_ctr");
    hope this helps.

  • How to get the count of each minutes

    Oracle 10 DB
    Hi All
    I using this query to get the count of records fetched by the concurrent program per min,
    select count (distinct b.attribute2)
    from oe_order_headers_all a,oe_order_lines_all b
    where a.header_id=b.header_id
    and b.flow_status_code ='AWAIT_QUINTIQ_BOOK'
    and to_Char(b.last_update_date,'DD-MON-YYYY HH24:MI:SS') between '19-JAN-2011 02:00:01' AND
    '19-JAN-2011 02:00:02'
    This is used to get the count of records fetched at one mins
    How to find the count of last 10 mins ie 1 mins count 2.00 to 2.10
    Thanks & Regards
    Srikkanth.M

    Try this:
    SELECT TRUNC(b.last_update_date, 'mi') time_mins, COUNT(DISTINCT b.attribute2)
    FROM   oe_order_headers_all a,
           oe_order_lines_all b
    WHERE  a.header_id = b.header_id
    AND    b.flow_status_code = 'AWAIT_QUINTIQ_BOOK'
    AND    b.last_update_date >= to_date('19/01/2011 02:00:00', 'dd/mm/yyyy hh24:mi:ss')
    AND    b.last_update_date < to_date('19/01/2011 02:10:00', 'dd/mm/yyyy hh24:mi:ss')
    GROUP BY TRUNC(b.last_update_date, 'mi');I'm not sure why you're bothering with the join to the oe_order_headers_all table, since you're not referencing that in the where clause or the selected columns list. I think you could probably do away with it.
    I've changed your code to take the last_update_date and truncate it to the minute level, and added that as a column, so you can see which count goes with which minute. Obviously, this has to be in the group by.
    I've also changed your date comparisons so that you're actually comparing dates to dates, rather than strings to strings - why would you deliberately take information away from the optimizer? Oracle does not treat strings the same as dates, and forcing it to use strings rather than dates could lead you to have a vastly differing execution path, which will most likely be less performant.
    Not to mention, coding like that leads you to assume that you can compare dates in strings without any problems, and that is just not the case. '19-FEB-2011' is counted as being earlier than '19-JAN-2008', as strings sort alphabetically.

  • EXPECTED OUTPUT:   here i need to get the count of 932 + 134 records = 1064

    Hi Team,
    could you pls suggest y iam getting wrong count.
    select * from i_invoice_info_t_log_v invlog,invoice_header_t invhead
    WHERE substr(invlog.tot_cust_no,1,7)=substr(invhead.acct_no,3,7)--------comparing tot_cust_no and acct_no for the other countries
    and invhead.comp_code NOT in (2300) ----here when iam running this query i am getting the count of 932 records.
    select * from i_invoice_info_t_log_v invlog,invoice_header_t invhead
    where substr(invlog.tot_cust_no,3,7)=to_char(substr(invhead.acct_no,3,7))--------------comparing tot_cust_no and acct_no FOR THE COUNTRY 2300
    and invhead.comp_code in (2300) ----Here when i am running this query i am getting the count of 134 records.
    EXPECTED OUTPUT:   here i need to get the count of 932 + 134 records = 1064 records.
    I am  using this query like below:
    select * from  i_invoice_info_t_log_v invlog,invoice_header_t invhead
    where
    (substr(invlog.tot_cust_no,1,7)=substr(invhead.acct_no,3,7)
               and invhead.comp_code NOT in (2300)
              OR
              (substr(invlog.tot_cust_no,3,7)=to_char(substr(invhead.acct_no,3,7))
               and invhead.comp_code in (2300)
    )---------------------------------------------------here when i am running this query i am getting the count of  18381 records which is incorrect.
    Expected output: i need to get the data for the comp_code which are not in 2300 and which are in 2300... both

    select  *
      from  i_invoice_info_t_log_v invlog,
            invoice_header_t invhead
      WHERE    (
                    substr(invlog.tot_cust_no,1,7) = substr(invhead.acct_no,3,7) --comparing tot_cust_no and acct_no for the other countries
                and
                    invhead.comp_code NOT in (2300) --here when iam running this query i am getting the count of 932 records.
            or (
                    substr(invlog.tot_cust_no,3,7) = to_char(substr(invhead.acct_no,3,7)) --comparing tot_cust_no and acct_no FOR THE COUNTRY 2300
                and
                    invhead.comp_code in (2300) --Here when i am running this query i am getting the count of 134 records.
    /SY.

  • SQ01 - SAP List Viewer - Ability to get the count of the output records

    Hello All,
    I have a problem with the SAP List Viewer output for an SAP Query.
    I have created a SAP query (SQ01) for a table. Its a basic query that queries on just one table (VTBFHA)
    When I run the query, for the output in the SAP List Viewer (ALV) format, Could anyone kindly help me figure out how to get the count of the number of records displayed in the output?
    For Eg: when we run the T code SE16, It gives us the number of records displayed in the output.
    Similarly, does anyone know if there is a way I could display the count of the number of records displayed in the output of the SQ01 query?
    I have tried checking the "Counter" in the basic list tab, but it does not give me the count.
    Any help will be greatly appreciated
    Thanks
    Subhani

    Yes there is a solution. Follow these steps
    Select your query
    Hit the button InfoSet Query
    Go to Edit->Settings->
    Check the box "Count in the Output list" in the Output tab
    Hit Save
    In pop up box select the user group
    hit enter
    Hit the back arrow (F3)
    now Execute the query
    You should be able to see the count in the output.
    The count will appear on the right most column of the output. If it does not go to the "Change layout" option and make sure it is part of the "Displayed columns list".
    Regards

  • How to find total count of records in a cursor

    Aassume below is the cursor i defined
    cursor c1 is select * from emp;
    now, i want to find the total count of records in this cursor using an existing function etc., using one line statement.
    FYI: c1%rowcount is always giving 0, so i cant rely on this.
    Any thoughts, please share.
    Thanks in advance.

    I am just showing this to show how to get the rowcount along with the cursor, if the program has so much gap of between verifying the count(*) and opening the cursor.
    Justin actually covered this, he said, oracle has to spend some resources to build this functionality. As it is not most often required, it does not makes much sence to see it as a built-in feature. However, if we must see the rowcount when we open the cursor, here is a way, but it is little bit expensive.
    SQL> create table emp_crap as select * from emp where 1 = 2;
    Table created.
    SQL> declare
      2   v_cnt     number := 0;
      3   zero_rows         exception;
      4  begin
      5    for rec in (select * from (select rownum rn, e.ename from emp_crap e) order by 1 desc)
      6     loop
      7        if v_cnt = 0 then
      8           v_cnt := rec.rn;
      9        end if;
    10     end loop;
    11     if v_cnt = 0 then
    12        raise zero_rows;
    13     end if;
    14   exception
    15    when zero_rows then
    16      dbms_output.put_line('No rows');
    17   end;
    18  /
    No rows
    PL/SQL procedure successfully completed.
    -- Now, let us use the table, which has the data
    SQL> declare
      2   v_cnt     number := 0;
      3   zero_rows         exception;
      4  begin
      5    for rec in (select * from
      6          (select rownum rn, e.ename from emp e)
      7          order by 1 desc)
      8     loop
      9        if v_cnt = 0 then
    10           v_cnt := rec.rn;
    11           dbms_output.put_line(v_cnt);
    12        end if;
    13     end loop;
    14     if v_cnt = 0 then
    15        raise zero_rows;
    16     end if;
    17   exception
    18    when zero_rows then
    19      dbms_output.put_line('No rows');
    20   end;
    21  /
    14
    PL/SQL procedure successfully completed.Thx,
    Sri

  • How to get the count of all the entries in the DB?

    I want to know the way getting the count of the entries in a DB. Not using cursor to visit all entries and record the count.
    Message was edited by:
    user633842

    Hello,
    Yes, depending on the type of database you are using, the
    following statistics from DB->stat, look like what you want.
    For Hash databases:
    hash_ndata;
    The number of key/data pairs in the database. I
    For Btree databases:
    bt_ndata;
    For the Btree Access Method, the number of key/data pairs
    in the database.
    For the Recno Access Method, the number of records in
    the database.
    For Queue databases:
    qs_ndata;
    The number of records in the database.
    Additional details are at:
    http://www.oracle.com/technology/documentation/berkeley-db/db/api_c/db_stat.html
    Thanks,
    Sandra

  • Any table or program to get the number of records in P, A and F tables

    HI all
    any table for program to get the number of records in P, A and F tables. I want to create SQ01 queries to get the status of number of records. We are gng for a production cutover next week. Want to capture all the data before and after Upgrade cutover.
    Also suggest me how to create sq01 queries.
    Thanks in advance
    regards
    Janardhan KUmar K.

    Use Transaction LISTSCHEMA to see all the tables assosciated with ur cube
    Total number would be what you find in both the E & F fact tables. If there is no compression in the cube then E table will be empty.
    Alternatively u can use se16 transaction and enter E table and F table manually
    E table - /BIC/E(Cube name) and Ftable -  /BIC/F(Cube name)
    Or else u can go to the manage of the cube and without selecting any field for O/P and ticking the option output number of hits execute. The total of Row Count will give u the total no of records in the cube.

  • Get the count of rows in a table control

    Hi Experts,
      How do I get the count of the rows in a table control during run time.
    I am developing a BDC in which I have to check all entries in a table control.
    My requirement is to get the total number of rows in a table control dynamically.
    Thanks
    Kumar

    Hi,
    Use a variable when u r passing the records from the internal table to the screen fields
    and display the same.
    I think this idea may help u.
    And pls explain me ur requirement clearly.
    Refer to the following link this may help u.
    http://sapabapnotes.blogspot.com/2008/03/working-with-ecatt-extended-computer.html
    Reward if helpful.
    Jagadish

  • How to get the number of records of a streaming result set

    Hi guys.
    So if it wasn't a streaming result set, I would have done this:
    {noformat}myResultSet<code class="jive-code jive-java">.last();
    {color:navy}*int*{color} numResults = </code>myResultSet<code class="jive-code jive-java">.getRow();
    </code>myResultSet<code class="jive-code jive-java">.beforeFirst();
    </code>{noformat}
    but being a streaming result set, beforeFirst() throws an exception...
    So how do you get the number of records in that result set? I wanna avoid an extra count(*) query, so I would appreciate other solutions than that.

    JoachimSauer wrote:
    vanwil wrote:
    you see, for now I just use a count(*) query to get the number of records, but that's adding a lot of extra waiting time...Iterating over the result twice will surely be slower then doing the count(*).great! so what I got now is actually the fastest way there is... awesome...
    If you get an exception, then you surely have a stack trace. That should tell you what happens, or at least where.com.mysql.jdbc.MysqlIO.checkForOutstandingStreamingData(MysqlIO.java:2066)
    Why do you need to know the number of elements beforehand, anyway?I need to know the number of elements because the incoming data goes into a table. Now of course, I could use ArrayList<String[]> or something, but wouldn't that require more memory resources than Object[][] ?
    No one can tell you that, at least not without more information (say, the stack trace for example).Here's the exception message:
    java.sql.SQLException: Streaming result set com.mysql.jdbc.RowDataDynamic@10c0ef2 is still active. Only one streaming result set may be open and in use per-connection. Ensure that you have called .close() on  any active result sets before attempting more queries.

  • Different ways to get the last 5 records ordered by date

    I have a query no that returns the tickets ordered by moddt, modification date,
    I was thinking of adding a select on top, to get the last 5 records of that list the select already generates. An other way was to use that select to create a view,
    and get the 5 last records that way. I also though of a possibly impossible cursor with a sequence, but hmm well I consider that not a good solution.
    So I'm trying to find a way, using a select, the topmost select which is not completed yet, to get the last 5 records. Maybe I should just try the rownumber thing again or something likewise.
    Select * from(
    select * from(
    Select ticketid, appliecatiecd, categorieid, substr(titel,&,200)||' ...' "titel", klantproriteitid, interneprioriteitid,
    (select g.voornaam||' '||g.naam
    from gebruiker g
    Where g.gebruikerid = t.gebruikerid
    And t.applicatiecd = NVL(:P0_applicatiecd, :F101_applicatiecd))"aangemaakt door",
    (select s.statusdefoms "status"
    From status s
    Where s.statusid = t.statusid
    And t.applicatiecd = NVL(:P0_applicatiecd, :F101_applicatiecd)) "status",
    Versieid,
    Moddt,
    Row_number() over (order by ticketid desc) rn
    From ticket t
    Where applicatiecd = NVL(:P0_applicatiecd, :F101_applicatiecd) )
    Order by moddt)
    Where --

    Hi Floris,
    You can also use RANK: http://www.dba-oracle.com/oracle_news/oracle_faq/faq_beg_sql_top_n_rows.htm
    Regards Pete

  • Getting the un updated records from receiver JDBC

    Hi Friends,
         I am doing the Proxy - jdbc integration. Consider my proxy sends 10 records to JDBC to update. Consider bcoz of some reason the jdbc updates only 8 records i need to get the 2 records back to Proxy which is not updated in the DB.
    Should i need to use the Proxy and JDBC channel as syn or seperate integration should be built. I have seen some documents, that we can get the count of the updated records from jdbc receiver, but i dont need the count i need the record which is not been updated.
    How can i do it. please give ur inputs.
    thanks
    Prem

    Unless  you are using SP on JDBC side and explicitly build a record set to send back the data it might not be possible. I am not sure if a custom record set could be returned in JDBC sync receive adapter. You can realize this by updating some other table on JDBC side and then polling that table to update ecc back.
    VJ

  • Trying to get the top N records from a query.

    Hello,
    Im trying to get the top N records of a random query. I've been looking for a decent solution for a while now and the 2 solutions I've come across are:
    select *
    from (select * from qrs_klantgroepen order by code) a
    where rownum < 10
    ORA-00907: missing right parenthesis
    The query works when I remove the order clause from the subquery, but it's mandatory to get the results I need.
    The next solution:
    select *
    from (select code, row_number() over (order by code) from qrs_klantgroepen) a
    where rownum < 10
    ORA-00923: FROM keyword not found where expected
    Apparently the construct used here isn't supported by my oracle version.
    I'm using oracle Oracle8 Enterprise Edition Release 8.0.5.2.1.
    Is there another way I can achieve the results I want?
    Looking forward to your replies.
    Frederik

    it must be nostalgy, but I would try something like
    SQL> select * from emp e where 5>(select count(*) from emp where ename<e.ename) order by ename;
         EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
          7876 ADAMS      CLERK           7788 1987-05-23_00:00:00       1100                    20
          7499 ALLEN      SALESMAN        7698 1981-02-20_00:00:00       1600        300         30
          7698 BLAKE      MANAGER         7839 1981-05-01_00:00:00       2850                    30
          7782 CLARK      MANAGER         7839 1981-06-09_00:00:00       2450                    10
          7902 FORD       ANALYST         7566 1981-12-03_00:00:00       3000                    20
    SQL> select * from (select * from emp order by ename) where rownum<6;
         EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
          7876 ADAMS      CLERK           7788 1987-05-23_00:00:00       1100                    20
          7499 ALLEN      SALESMAN        7698 1981-02-20_00:00:00       1600        300         30
          7698 BLAKE      MANAGER         7839 1981-05-01_00:00:00       2850                    30
          7782 CLARK      MANAGER         7839 1981-06-09_00:00:00       2450                    10
          7902 FORD       ANALYST         7566 1981-12-03_00:00:00       3000                    20Message was edited by:
    Laurent Schneider

  • How can i get the count vaule from GPIB?

    I want to get the count waule from GPIB ,but I find that the vaule I get from GPIB now is the trace vaule ,
    so I ask How can i get it.

    The count of what? What kind of instrument are you using?

Maybe you are looking for

  • Down payment request and special GL indicator

    Hi SAP Experts, From several days I'm looking for an explanation of standard down payment process in SAP: When we create down payment request for customer, billing type FAZ, corresponding FI document is created with special GL indicator "F". Does som

  • Laps with no invoice or cds?

    Hello there, This is my first post and I'd like some guidance before buying my 1st lap. The country I live has no Tos factory so all laps are imported from the US (OS in English). But some stores here sell them without the back-up cd, they informed I

  • Does premiere elements 13 support canon mov files?

    Does Premiere Elements support Canon mov files or must I convert the mov file to a different format first?  If I have to convert the mov file first, what file conversion program should I use and what file type should I convert it to?

  • Google Calendar sync won me over

    I am a MobileMe user, and recently tried Google Sync. I am hoping someone who uses either can chime here and confirm my finding. MobileMe will not sync with my work laptop because my Outlook client is configured to sync with Exchange. Google Sync, ho

  • Pixelated jpg image files

    Muse 7.2, OSX 10.9.2 I have inline images and a gallery slideshow on a site.  Some of these images are extremely pixelated on the rendered web page.  All the images were sized in Photoshop to the exact pixel dimensions and saved as  saved-for-web jpe