Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-08-31
7
Medium Priority
?
32 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 53

Expert Comment

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

END

Open in new window

0
 
LVL 53

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
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!

 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
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 syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

610 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