Skip to Main Content
  • Questions
  • Gather_table_stats when UPDATING rows in a table using rownum as predicate

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, paras.

Asked: August 28, 2016 - 8:51 pm UTC

Last updated: August 29, 2016 - 1:12 am UTC

Version: 12

Viewed 1000+ times

You Asked

Hello Tom,

I came across the structure where in users creating the table or updating the table uses the predicate as
"rownum exec dbms_stats.gather_table_stats(xx,xx)". Can you help us to explain how this predicate structure how it works in Create Table and Update Table statements. Below are the two example mentioned. I am assuming that this will gather table statistics but the doubt is how/why to use in where clause as predicate?

======================================
Example1:-
create table tab as
select object_id as col from all_objects
where rownum exec dbms_stats.gather_table_stats(ownname=>user, tabname=>'TAB');

=======================================
Example2:-
update tab set col = 1
where rownum exec dbms_stats.gather_table_stats(ownname=>user, tabname=>'TAB', cascade=>true);

How many rows in the table will update here since use of this predicate? will this update all the records or how much ?
========================================

I've tried searching over the internet to find the explanation with respect to this but no luck. If you can explain or point me to some direction of this how the gathering the table statistics works while used in predicates. Thank you in advance.

and Connor said...

I think you have intercepted the SQL incorrectly, or there is something missing, because that is not a valid construct

SQL> create table tab as
  2  select object_id as col from all_objects
  3  where rownum exec dbms_stats.gather_table_stats(ownname=>user, tabname=>'TAB');
where rownum exec dbms_stats.gather_table_stats(ownname=>user, tabname=>'TAB')
             *
ERROR at line 3:
ORA-00920: invalid relational operator


In particular, the 'exec' command applies to the SQLPlus tool only, not general SQL.

If I had to guess, the command has been truncated, so someone ran something like:

SQL> create table tab as
  2  select object_id as col from all_objects
  3  where rownum <= 1000;

SQL> exec dbms_stats.gather_table_stats(ownname=>user, tabname=>'TAB');


and only the bit up to the "rownum" got picked up.

Hope this helps.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

paras shah, August 29, 2016 - 1:29 am UTC


More to Explore

DBMS_STATS

More on PL/SQL routine DBMS_STATS here