• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 41
  • Last Modified:

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

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
newdeveloper india
Asked:
newdeveloper india
  • 2
  • 2
  • 2
1 Solution
 
Ryan ChongCommented:
CREATE PROCEDURE YourSP
AS
BEGIN
	
	select * into tablenm from tblnm2;

END

Open in new window

0
 
Ryan ChongCommented:
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
 
newdeveloper indiaAuthor Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
johnsoneSenior Oracle DBACommented:
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
 
johnsoneSenior Oracle DBACommented:
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
 
newdeveloper indiaAuthor Commented:
Thank you sir...
It helped in some extent.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now