Solved

dynamically creating view in a stored procedure

Posted on 2013-07-01
5
414 Views
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)

as

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

               from table where table.id = p_id;'
0
Comment
Question by:cookiejar
  • 2
  • 2
5 Comments
 
LVL 76

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,
                          col2

               from table where table.id = ' || p_id;

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

Accepted Solution

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

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

Open in new window

0
 
LVL 76

Expert Comment

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

NEVER use LONG's.  Use a CLOB.

>>id='||p_id||';'

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?
0
 
LVL 48

Expert Comment

by:PortletPaul
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.
WHY?

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:
CREATE OR REPLACE VIEW ...
0
 
LVL 35

Expert Comment

by:YZlat
ID: 39294243
slightwv, you posted

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

               from table where table.id = ' || p_id;
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Single ERP VS muttiple Application or Systems 6 38
Pl/SQL Query 31 61
Last record chosen in Oracle Query 3 34
PL/SQL LOOP CURSOR 3 40
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now