Solved

create table in procedure having query like 'create table tablenm as (select * from tblnm2);

Posted on 2016-08-31
7
29 Views
Last Modified: 2016-09-27
Please help me for this type of table creation..


create table in procedure having query like

'create table tablenm as (select * from tblnm2);

how to bind this query in procedure ??
0
Comment
Question by:newdeveloper india
[X]
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
  • 2
7 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41777701
CREATE PROCEDURE YourSP
AS
BEGIN
	
	select * into tablenm from tblnm2;

END

Open in new window

0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41777706
you may also use this for better handling:
CREATE  PROCEDURE YourSP
AS
BEGIN
	
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'tablenm') AND type in (N'U'))
		DROP TABLE tablenm

	select * into tablenm from tblnm2;

END

Open in new window

0
 

Author Comment

by:newdeveloper india
ID: 41777775
I have only one procedure to create in which I want to insert,update,alter and create table from my select query  and then update this table with respective values and then insert those values in another table. This solution will help in another procedure.
Thank you Sir..
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:johnsone
ID: 41777911
It would appear that this question is posted in the Oracle zone.

Not sure what database that procedure is intended for, but it certainly isn't Oracle.  SYS.OBJECTS isn't a valid object in Oracle and if it was a regular user wouldn't have privileges on it, not sure what OBJECT_ID is (probably a function), you cannot create a table that way, and you cannot execute DDL directly in PL/SQL.

For Oracle, I would do it this way:
create or replace procedure p1 as
  already_exists exception;
  pragma exception_init(already_exists, -955);
begin
  execute immediate 'create table tablenm as (select * from tblnm2)';
exception
  when already_exists then
    execute immediate 'drop table tablenm';
    execute immediate 'create table tablenm as (select * from tblnm2)';
end;
/

Open in new window

0
 
LVL 35

Accepted Solution

by:
johnsone earned 500 total points
ID: 41777918
OK, I have no idea what I was thinking.  I knew that procedure didn't look right and I shouldn't be writing code this early.  While the other one would work, this one is better:
create or replace procedure p1 as
  not_exists exception;
  pragma exception_init(not_exists, -942);
begin
  begin
    execute immediate 'drop table tablenm';
  exception
    when not_exists then
      null;
  end;
  execute immediate 'create table tablenm as (select * from tblnm2)';
end;
/

Open in new window

0
 

Author Comment

by:newdeveloper india
ID: 41778150
Thank you sir...
It helped in some extent.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
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 video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

724 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