0

I'm trying to loop through a set of views and gather the view name and counts. I created a new table and added an ID columns. I'd like to loop through my sql statment and retrieve the values while the ID < 10.

Here is my create table statement I'm using to set my id column;

create table  View_Table AS 
select VIEW_NAME,
row_number() over ( order by VIEW_NAME ) as id
from all_views;

--My attempt to loop through the sql statement

DECLARE
     x NUMBER := 0;
    BEGIN
      LOOP
        select view_name,count(*) from VIEW_TABLE where id = x group by VIEW_NAME;
        x := x + 1;  
      EXIT WHEN x > 10;
     END LOOP;
   END;

---Here is the error message I'm receiving:

Error starting at line : 13 in command -

DECLARE
     x NUMBER := 0;
    BEGIN
      LOOP
        select view_name,count(*) from VIEW_TABLE where id = x group by VIEW_NAME;
        x := x + 1;  
      EXIT WHEN x > 10;
     END LOOP;
   END;
Error report -
ORA-06550: line 5, column 9:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

Any help would be appreciated.

2
  • Your count(*) is always going to be 1, since each view appears in your new table once (unless you have the same view in multiple schemas). Are you actually trying to count the number of rows in each view - which you would have to do with dynamic SQL? Commented Jun 6, 2015 at 10:57
  • I was trying to get the number of rows in each view and pass in the view name to my dynamic sql statement. Commented Jun 9, 2015 at 21:18

1 Answer 1

2

When you have a select statement in a PL/SQL block, you have to provide somewhere to store the results. Try this.

DECLARE
     x NUMBER := 0;
     cnt number;
     vname varchar2(100);

    BEGIN
      LOOP
        select view_name,count(*) 
        into vname, cnt
        from VIEW_TABLE where id = x group by VIEW_NAME;
        x := x + 1;  
      EXIT WHEN x > 10;
     END LOOP;
   END;

Is this just a learning exercise? Because you don't do anything with the results. I would recommend also reading Steve Feuerstein's articles. Lots of good stuff in there.

Sign up to request clarification or add additional context in comments.

3 Comments

Also note from the CREATE TABLE in the question that id will start at 1, so the first time through the loop there will be a NO_DATA_FOUND exception because the query is looking for id = 0. I'd recommend initializing x to 1 instead of zero, or moving the x := x + 1 to come before the select.
This was just a learning exercise for me. I just needed to get a simple block of dynamic sql working and I can build onto that. Thank you all so much for your help.
Thanks for the link to these article. Incredibly helpful.

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.