Solved

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

Posted on 2016-08-31
7
21 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
  • 2
  • 2
  • 2
7 Comments
 
LVL 49

Expert Comment

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

END

Open in new window

0
 
LVL 49

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

 
LVL 34

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 34

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

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to recover a database from a user managed backup

747 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

9 Experts available now in Live!

Get 1:1 Help Now