dynamically creating view in a stored procedure

Posted on 2013-07-01
Last Modified: 2013-07-22
How do I create a view in a stored procedure.  The stored procedure is passed a parameter, for example an id,  I will need to create the view name from this id and create the select statement using the id as a parameter in the where clause.  How is this done?

create replace sp_create_view(p_id number)


string = 'create view dyn' + p_id ' as
               select col1,

               from table where = p_id;'
Question by:cookiejar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39291297
First, I strongly encourage you to not do DDL in a procedure.

What is the view supposed to save you?

That said, given the example above (you have a typo):

string := 'create view dyn' + p_id ' as
               select col1,

               from table where = ' || p_id;

--after you build the string
execute immediate string;
LVL 35

Accepted Solution

YZlat earned 500 total points
ID: 39291501
Try something like that:

PROCEDURE usp_create_view(p_id NUMBER) IS
   v_sql LONG;
   v_sql := 'CREATE VIEW dyn'||p_id ||' AS SELECT col1, col2, FROM TABLE WHERE'||p_id||';'
   DBMS_OUTPUT.PUT_LINE('v_sql: ' || v_sql);

Open in new window

LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39291657
>>v_sql LONG;

NEVER use LONG's.  Use a CLOB.


I believe adding the semi-colon as part of the actual statement will generate a:
ORA-00911: invalid character

All that said, isn't that pretty much what I posted?
LVL 48

Expert Comment

ID: 39292245
>>I will need to create the view name from this id and create the select statement using the id as a parameter in the where clause.

what do you possibly gain by building a view, then using that view as part of a select?

just go to the select and bypass the create/replace of the view

seems to me you have the impression that this will (somehow) be more efficient - it won't be
also seems to me you feel that a parameter is a necessary part of efficiency - it isn't
(a 'where clause' on a STATIC view could be just as efficient, perhaps more so)

Would you describe what it is you are trying to achieve please?

and although I really don't like the sound of this, if you are going to proceed down this path, use:
LVL 35

Expert Comment

ID: 39294243
slightwv, you posted

string := 'create view dyn' + p_id ' as
               select col1,

               from table where = ' || p_id;

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle collections 15 37
Fill Null values 5 36
Oracle cursor lifecycle inside procedure. 2 24
error starting form builder in 11g 2 24
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question