Get the record count from a query

Hi,
does anyone know how to get the record count from a sql query?
e.g: I've got a ResultSet from the Statement.excuteQuery(), and I want to know how many records this ResultSet contains, how to get this?
I'd read thoughout the documents of Statement and ResultSet, but couldn't find a solution, do I have to use another seperate query such as "select count(*)" to do this?
thanks.
Yang Liu

If you are not using a scrollable result set then the following is the best way to do it.
there are several key words in SQL that can be used, the one you are interested in is count();
so if your query at the moment is
"select col1, col2, col3 from my_table where col2=? and col3=?"you can work out how many rows will be returned by executing this command first
"select count(col1) from my_table where col2=? and col3=?"this will return a result set with one row and one column, you can get the row count as follows:
ResultSet rs = ps.executeQuery();
int rowCount = rs.getInt(1);I hope this helps :)

Similar Messages

  • Getting the record count from result set

    i'm retreiving the result set using the executeQuery method, now i want to know how many records are there in the result set, that is the record count of the result set.
    one solution to that is to first use the executeUpdate and then use the executeQuery but i think that is not the right way.
    so please tell me is there any method in jdbc to get that thing done
    Tanx

    Hi
    Do you know if your DB supports "insensitive scrolling"?
    SQL generally do, but some don't - I had the same problem with the
    open source version of Interbase from Phoenix...
    Anyway - try creating your statement this way:
    public Statement createStatement(int resultSetType, int resultSetConcurrency)
    throws SQLException
    ...where resultset type should be:
    ResultSet.TYPE_SCROLL_INSENSITIVE
    Then you can do this:
    ResultSet rs = stm.executeQuery(q);
    int size = rs.last(); //this what you looking for?
    rs.beforeFirst();
    while(rs.next()){
    }

  • How to return the record count of a query to a number variable

    How would I get the record count from a query without using a cursor to step through the query and count the records individually?
    query: select * from table1 where column1 = column2
    How would I get the record count of the query above?
    thanks,
    michelle

    I figured it out
    select count(*) into theCount
    from ( select * from table1 where column1=column2 );
    thanks,
    michelle

  • File Adapter - how to get the file count from a folder

    Hi All,
    I have a requirement that have to poll a directory when the file count is reached to number N (ex:number of files avilable in folder is 5) otherwise it should wait and not pick any of the files. Is it possible to get the file count from a folder using file adapter ?? otherwise please suggest me an approach to achieve this requirement.
    Thanks,
    JJ

    Hi Sarath,
    Thank you for your reply.
    Go with the list files operation of file adapter it will gives you the number of files in the specified folder as you given. . - this step is already done.
    When the number of files reaches your count startup your webservice that which can polls the files. . . - how can i acheive this?? Have to poll the directory and process the number files - please let me know, what could be added to the webservice which is being invoked after cheking file count from parent process.
    The reason for the above question is - we cannot use ReadFile operation in second webservice because it will be automatically triggered when the file is avilable. Also SyncRead operation supports reading one file in b/w bpel process. Kindly explain me the implementation steps.
    Thanks,
    JJ

  • How to get the FILE COUNT from File directory

    Hello,
    i have to develop a scenario like, get  the file count from source file directory and validate whether the file count is 5 or not. if 5 files exist i need to process those 5 files to DB tables. if file count is not equal to 5 then i need to send a mail to customer that files are missed at source directory. (subject as files were missed at source directory and in content i need to display the file names exist at source file directory. So that missed file will be generated by the customer based on this mail).
    Could you please let me know how to get the count of files from source file directory. if it is possible only with UDF please provide the Java code
    Best Regards,
    SARAN

    Do these files have some fixed names?
    Can you try to use the option Advanced Selection For Source File to make XI  pick all 5 files in one shot?
    Check this blog on the same -
    /people/mickael.huchet/blog/2006/09/18/xipi-how-to-exclude-files-in-a-sender-file-adapter
    If this is not a option - BPM sounds the only possible way.
    Regards,
    Bhavesh

  • How to get the record count printed for the report in the Dashboards

    Hi,
    I would like to get the record count printed at the bottom of every dashboard report like:
    < 1 - 25 of 6300 > instead of < 1 - 25 >
    Any help is appreciated
    Regards
    B

    I have tried the following formula which identifies the lowest grain but it does not seem to give me correct result.. I am not getting the correct count. I am getting as 3.A work order can be updated only once at one point of time.hence the formula
    MAX(RCOUNT(CAST("SR Wo Fact"."Crm Wo Number" AS CHAR) ||CAST("SR Wo Fact"."PSC Timestamp" as char)))
    I tried only MAX(RCOUNT(1)), but I was able to get the record count as corect for Administrator but not for other users.
    Has anyone come across this scenario.
    Thanks Shravan
    Edited by: 786443 on Aug 19, 2010 10:22 AM

  • Get the record count

    The Query below is part of a huge query. The snipet is just the part that I need to make a change.
    For the query below, how can I get the recordcount in the select inside the case statement? I want to only output the r_text when the record count of the select is zero (0) else make the output to r_text null
    select
    distinct a.style_id, b.sty, b.season, a.cust, a.ad_d, b.code,
    (case when b.code = 'MAKE' then
    (select min(t.value) from sn.age r, sn.text t
    where r.t_id = '18' and r.name = 'MTGNSL'
         and r.t_id = t.t_id
         and r.name = t.name and r.text_id = t.text_id
         and r.id_1 = a.id
    ) end) r_text,
    (case when b.code is null then 'Blank Code'
    when b.code = 'MAKE' then 'MAKE' end) err
         

    If you are not using a scrollable result set then the following is the best way to do it.
    there are several key words in SQL that can be used, the one you are interested in is count();
    so if your query at the moment is
    "select col1, col2, col3 from my_table where col2=? and col3=?"you can work out how many rows will be returned by executing this command first
    "select count(col1) from my_table where col2=? and col3=?"this will return a result set with one row and one column, you can get the row count as follows:
    ResultSet rs = ps.executeQuery();
    int rowCount = rs.getInt(1);I hope this helps :)

  • Getting a record count in a query using UNIQUE

    I need to know how to grab a record count for a query so I can pass it back into the recordset.
    This select query uses UNIQUE to rollup the 555 records to 125 unique rows.
    But how would I do a record count.
    Do I pass the query to a cursor and then count the records in the cursor?
    Please detail your help please.
    Thank you in advance.

    SELECT unique
         CE.DATE_OF_SERVICE AS DOS_FROM
         ,CE.SERVICE_END_DATE AS DOS_TO
         ,CE.EVENT_SERVICE AS SERVICE_TYPE
         ,CN.CREATE_DATE_TIME
         ,CN.CASE_NOTE_ID
         ,CN.CASE_ID
         ,CN.EVENT_ID
         ,CN.REASON
         ,CN.CREATOR_USER_ID AS CREATOR_ID_USED
         ,CN.ROWID AS ROW_ID
         ,(SELECT Retrieve_Note2(CN.CASE_ID,CN.CASE_NOTE_ID) from DUAL) AS NOTE
    FROM
         MEMBER_TO_PATIENT MTA,
         CARE_EVENT CE,
         CASE_NOTES CN
    WHERE
         MTA.SUBSCRIBER_LID = '1260016473015' AND
         MTA.CASE_ID = CN.CASE_ID AND
         CN.EVENT_ID = CE.EVENT_ID
    ORDER BY
         CN.CASE_NOTE_ID DESC

  • Get the records number from internal table with conditon.

    Internal table itab got more than 1000 records,now i need to get the number of records with condition that itab-field1 = 'XXXX'.
    actully, i got an inefficient logic to count the number in a loop statement. is there better way to implement it?

    Hi,
    One alternative solution, which may be useful for other parts of your program, is to have an aditional itab1 with a single column/field (field1), which acts like a index or shadow table, and which is filled simultaneously with itab. Then you can use DESCRIBE TABLE itab1 LINES lv_lines.
    Atb

  • How to get the record count before fetch

    CREATE PACKAGE demo_cur_pkg AS
    TYPE EmpName IS RECORD (name VARCHAR2(10));
    TYPE cur_type IS REF CURSOR RETURN EmpName;
    PROCEDURE open_emp_cur (
    curs IN OUT cur_type,
    dept_num IN NUMBER);
    END;
    CREATE PACKAGE BODY demo_cur_pkg AS
    CREATE PROCEDURE open_emp_cur (
    curs IN OUT cur_type,
    dept_num IN NUMBER) IS
    BEGIN
    OPEN curs FOR
    SELECT ename FROM emp
    WHERE deptno = dept_num
    ORDER BY ename ASC;
    END;
    END;
    After this package has been stored, you can open the cursor curs by calling the open_emp_cur stored procedure from your Pro*C/C++ program, and FETCH from the cursor in the program. For example:
    sql_cursor emp_cursor;
    char emp_name[11];
    EXEC SQL ALLOCATE :emp_cursor; /* allocate the cursor variable */
    /* Open the cursor on the server side. */
    EXEC SQL EXECUTE
    begin
    demo_cur_pkg.open_emp_cur(:emp_cursor, :dept_num);
    end;
    EXEC SQL WHENEVER NOT FOUND DO break;
    for (;;)
    EXEC SQL FETCH :emp_cursor INTO :emp_name;
    printf("%s\n", emp_name);
    Any solutioon is welcome

    Fail to understand why you want to introduce exception processing - e.g. use methodA for less than 10,000 rows and use methodB for 10,000 and more.
    Why not use methodC - optimal support for fetching, irrespective of whether there are 1 row to fetch, or a 100,000 rows?
    There is no best way for fetching a small number of rows versus a different best way for fetching more rows. There is a single optimal method - and this remains the same irrespective of the number of rows being returned.

  • How to get the desire output from sql query

    Consider an Order Table: ...
    Order Table:
    Order Id Item Qty
    O1 A1 5
    O2 A2 1
    O3 A3 3
    Please provide SQL which will explode the above data into single unit level records as shown below
    Desired Output:
    Order Id Order Id Qty
    O1 A1 1
    O1 A1 1
    O1 A1 1
    O1 A1 1
    O1 A1 1
    O2 A2 1
    O3 A3 1
    O3 A3 1
    O3 A3 1

    How do I ask a question on the forums?
    SQL and PL/SQL FAQ

  • How to count no of records returned from a query?

    Hello,
    I need to find out how many records displayed from a query without writing any other queries.
    i mean that.
    I have a query such as:
    query = "select * from users";
    and the resultset for it is rs.
    and i need to display how many records fetched from this query without using any other query such as "select count(user_id) from users" etc.
    Is there any function ?

    No, unfortunately there is no method or property that will tell you how many rows have been returned by a query.
    People use many different techniques.
    1.The simplest would be, keep incrementing a counter in while ( rs.next() ) and at the end you'll have the count. But only at the end, after the loop.
    2. Run another query with count(*) just to fetch the count.
    3. Run the same query twice, once for counting and then again for printing out. Very dumb idea though, might as well do 2. suggested above.
    4. If you have a scrollable resultset, you could go to the lastRecord and then getRow number and hence know how many there are and then rewind the resultset. I don't know how advisable using scrollable resultsets is.
    5. Create a slightly clumsy query like : select count(*) as "totalcount", emp_id, emp_name, emp_email, emp_phone from employees group by emp_id, emp_name, emp_email, emp_phone. You'll need to use the group by clause since count() is an aggregate function and you can't mix aggregate functions and columns unless you add the group by to all the columns you're fetching.

  • How to get the time length from send record command to record video really?

    I have two buttons. Record/Stop
    I press Record button to record the video and press Stop
    button to stop recording.Now,I spend 1986 ms from press Record
    button to press Stop button.
    But the flv file's length is 1920 or 1910 or 1915 ms.
    So I want to get the time length from send record command to
    record video really.
    Can you give me some suggests.
    Thanks!

    maybe you have lag in publishing.... are you try watch de
    NetStream.liveDelay() method?
    or start counting when NetStream.onStatus has
    "NetStream.Record.Start" and "NetStream.Record.Stop" in information
    objects....

  • How do I get a page count from the HP Photosmart 5514?

    How do I get a page count from the HP Photosmart 5514?

    Simple.  Access the printer web gui by typing in the printers IP address into your browsers address bar.  Select Tools/Usage reports.  Done.

  • Get the min value from a record set

    RDBMS 9.2.0.6
    What is the best (fast) way to get the min date
    from a record set.
    For example:
    table tab (col1 NUMBER, col2 DATE ) with 47697857 records.
    I would like to get the MIN(col2) where col1 = XXXX

    I would create an index on col1, col2. If you are only returning the date and possibly col1, then Oracle will not even need to look at the table, all the required data is in the index. Even if you want additional columns from the tble, Oracle should pick the index scan in most cases, unlessyou are looking for a significant fraction of the rows in the table.
    HTH
    John

Maybe you are looking for

  • Incorrect and Un-Changeable 'Video Kind' for Videos Purchased From iTMS

    Since I can't seem to find any other way to contact Apple except through paid support channels, their suggestion is to post something here. Hopefully this re-wording of other posts I've read will shed more light on the problem. To Apple: Some videos

  • Table control in Screen

    Hi, I have designed an interface like SM31 to maintain a Z-table. My requirement is that I don't want to make Key fields input-enabled until user want to insert a new record and when user clicks on button in toolbar to insert a new row in table contr

  • DBCascade Delete

    The mechanism of DBCascade Delete is controlled by Application server or Application server use Database "DBCascade Delete" mechanism to do the action. Because I use WLS6.1 "Create Default DBMSTables" mechanism to create two tables which the relation

  • Client management with nio

    Hi everyone! I have a program based on nio which has a selector thread that handles the accepts and reads fromt he clients. Another thread, the main one, get's all the read information, handles it and sometimes has to send something back. Now I wonde

  • Address Bar Dropdown Option like IE

    I know that I should be thinking outside of the "Windows Box" but my wife liked using the drop down arrow in IE and finding her previous websites that way. She is begining to get use to typing in the begining part of the site and Safari showing it be