1

I have a sql query with where condition as ROWNUM=10. And the query result i store it in one GTT table. But it is possible that the sql query is fetching more records than the mentioned WHERE condition i.e. ROWNUM=10.

So i wanted to know, is the query fetching >10 records are not.

This i can achieve by executing the same query twice i.e. once time to know the count and second time to insert the records into the gtt table.

But it is not an good idea to run the query twice.

So can any one help me to find the count of the sql query with out executing it twice.

3
  • share the query...when u say rownum=10, it would definitely return only 10. If not, the position of it is wrong. Commented Feb 27, 2018 at 9:19
  • 1
    where rownum = 10 will not return any rows because of how rownum works. And where rownum<=10 won't insert more than 10 rows. An MCVE would help us understand what you are actually doing and the issue you have. Are you trying to see how many rows the query would find without that condition, but still only insert the first 10 rows into the GTT? Commented Feb 27, 2018 at 10:35
  • @AlexPoole, Yes, i am tying to find how many rows the query would find with out ROWNUM condition but insert only 10 rows into gtt. Please note that i have dynamic SQL query and while inserting into GTTi use execute immediate insert into gtt select *from query were rownum<=10 Commented Feb 27, 2018 at 11:02

3 Answers 3

3

If you are inserting those records in the GTT table, and you want to know how many rows you have selected/inserted, you could use SQL%ROWCOUNT

Begin
    INSERT INTO GTT_TABLE
         SELECT *
           FROM QUERY_VIEW
          WHERE Condition() = '1';

    If SQL%ROWCOUNT > 10 Then
        dbms_output.put_line('Query returns ' || SQL%ROWCOUNT || ' rows.');
    End if;
End;
Sign up to request clarification or add additional context in comments.

2 Comments

If in the select itself i have given ROWNUM<=10 then isn't the SQL%ROWCOUNT will also return 10??
@teepu yes, it would return 10 (or less, if you select less than 10 rows). You can't extract more than 10 rows with that clause
0

You can use the solution found at https://stackoverflow.com/a/17206119/7676742 to get records and count of these records together.

SELECT COUNT(*) OVER (), c.*
FROM CUSTOMER c 
WHERE c.Name like 'foo%';

2 Comments

i have query like execute immediate insert into gtt select *from query were rownum<=10. But i wanted to know with out this ROWNUM condition what would be the count of the select statement .
you want to get count of query without where clause and also result of query with where clause at the same time, right?
0

You could open a cursor for the query without the rownum condition and fetch until you run out of data or hit an 11th row:

declare
  l_query varchar2(4000) := '<your query without rownum condition>';
  l_counter pls_integer := 0;
  l_cursor sys_refcursor;
  l_row gtt%rowtype;
begin
  open l_cursor for l_query;
  loop
    fetch l_cursor into l_row;
    exit when l_cursor%notfound;

    l_counter := l_counter + 1;
    if l_counter > 10 then
      dbms_output.put_line('Query got more than 10 rows');
      exit;
    end if;

    -- first 1-10 rows so insert
    insert into gtt values l_row;
  end loop;
end;
/

Or with a collection to make it slightly more efficient:

declare
  l_query varchar2(4000) := '<your query without rownum condition>';
  l_cursor sys_refcursor;
  type t_tab is table of gtt%rowtype;
  l_tab t_tab;
begin
  open l_cursor for l_query;
  -- fetch gets at most 10 rows
  fetch l_cursor bulk collect into l_tab limit 10;
  -- up to 10 rows found are inserted
  forall i in 1..l_tab.count
    insert into gtt values l_tab(i);

  -- fetch again, if it finds anything then total row count > 10
  fetch l_cursor bulk collect into l_tab limit 1;
  if l_cursor%found then
    dbms_output.put_line('Query got more than 10 rows');
  end if;

  close l_cursor;
end;
/

However, the optimiser can often use a rownum condition to reduce the work it has to do (via a stopkey which you can see in the execution plan). It might still be faster and more efficient to run the query twice, once with the 10-row limit for the insert, and again with an 11-row limit to just get the count, and see if that is 11 or not. You should test both approaches to see which is actually better for you data. (And any others that are suggested, of course!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.