1

I have a list of people and I want to create view for each person. If I Have 1 person, my view would be:

CREATE VIEW PersonInfo AS (
    SELECT * FROM People WHERE id = 1000
);

But in fact, I have thousands of people, I want to create a stored procedure in Oracle to create view for each person, But View names are duplicated when I use it. How can I handle that problem? Thank you for helping in advance.

2
  • 5
    One has to seriously question the basic premise - the need to create a separate view for each person. What business requirement drives you to this "solution"? This has all the earmarks of a typical x-y question Commented Mar 12, 2022 at 15:00
  • 1
    What do you mean by "person"? Is it an Oracle user? Commented Mar 12, 2022 at 15:55

2 Answers 2

4

Your aim is interesting(presumably a related with training but not a real-world scenario), but a dynamic solution would be handled with a for loop by using EXECUTE IMMEDIATE of PL/SQL such as

SQL> CREATE OR REPLACE PROCEDURE Crt_People_Views AS 
BEGIN
 FOR p IN ( SELECT id FROM People )
 LOOP
   EXECUTE IMMEDIATE 'CREATE OR REPLACE VIEW PersonInfo_'||p.id
                   ||' AS SELECT * FROM People WHERE id = '||p.id;
 END LOOP;
END;
/
SQL> EXEC Crt_People_Views; -- call the proc. which will create the desired views
Sign up to request clarification or add additional context in comments.

Comments

-1

What problem are you trying to solve by creating a view for every person?

Would it make more sense to create a single view that takes a parameter (person_id)?

Something like this?


CREATE OR REPLACE VIEW VIEW_ABC (parm1 INTEGER) AS
  SELECT *
  FROM   XYZ
  WHERE ….

Call like this.

Then, all we need do is,
SELECT *
FROM   VIEW_ABC (101)
/

No probs. with bind variables. Nicely integrated as one would expect.

1 Comment

I don't believe that's valid syntax. Views can't have parameters (there is nothing in the 21c create view syntax diagram about parameters docs.oracle.com/database/121/SQLRF/…). Perhaps you're thinking of a pipelined table function that takes a parameter?

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.