Link to home
Start Free TrialLog in
Avatar of newdeveloper india
newdeveloper india

asked on

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 ??
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

CREATE PROCEDURE YourSP
AS
BEGIN
	
	select * into tablenm from tblnm2;

END

Open in new window

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

Avatar of newdeveloper india
newdeveloper india

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of johnsone
johnsone
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you sir...
It helped in some extent.