Solved

oracle create table if not exist

Posted on 2014-03-06
7
5,067 Views
Last Modified: 2014-03-11
In Oracle 8i - how can I create a table if not exist, and if it exist drop table and then create it?

Please let me know
0
Comment
Question by:CalmSoul
  • 4
  • 2
7 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39911109
When I see things like this it sort of implies something like a TEMP table.

You don't need to do this starting in 8i.  You create what is called a Global Temporary Table just once.

Then applications can insert into them and only the session that inserts into it can see the data.

As far as the question goes:
just try to create it and ignore any error generated.


If you can provide some context into exactly what you are doing, we can offer better advice.
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 39911131
You make simple things complicated :)

This is my create table script

create table tab(
IDTABLE VARCHAR2(10));

Open in new window

0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39911159
I understand the create table syntax.

I don't understand the context behind the need to check for it if it already exists.

Are you creating a .SQL script as part of some application install and need to see if it has already been executed before?

Are you creating a PL/SQL stored procedure to does something?

Are you doing something else?

Not trying to make it complicated, just trying to understand the requirements and the environment we are working in.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Author Comment

by:CalmSoul
ID: 39911207
PL/SQL stored procedure because I have to run various queries against this table and has to fill in with content using different data sets
0
 
LVL 23

Expert Comment

by:David
ID: 39911241
Trying to be calm too :)  but let's try:

IF the table does not exist, THEN create it, ELSE (if exists) TRUNCATE contents.

INSERT into the table, COMMIT rows, then SELECT data.

1.  Would the table definition ever change between sets, such as having different columns, data types, constraints?  In which case, you're right to DROP/CREATE the table rather than to truncate it.

2.  If yes, would it be suitable to define the data set as a row%TYPE?
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39911265
>>because I have to run various queries against this table and has to fill in with content using different data sets

This is EXACTLY what global temporary tables were created for.

You create them ONCE outside of any code.  Then any session can insert data into them.  Only the session that inserts data into them can see the data they inserted.  Then when the session disconnects, the data is automatically deleted.

The 8i docs on it:
http://docs.oracle.com/cd/A87860_01/doc/index.htm

We 'can' show you how to drop/create and trap errors in PL/SQL but based on what you posted, I don't want to provide the example since I believe it is the wrong approach.
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 39911272
Here is a global temp table example to show it in action.

The setup:
drop table tab purge;
create global temporary table tab(IDTABLE VARCHAR2(10)) on commit preserve rows;

Open in new window


Open two sqlplus windows and connect to the database in both of them.

In window1:
insert into tab values('Hello');
commit;
select * from tab;

Open in new window


In window2:
insert into tab values('World');
commit;
select * from tab;

Open in new window


In Window1, reconnect (do not exit and rerun sqlplus) to the database using the same username and reissue the select:
connect username/password@database
select * from tab;

Open in new window


What are the results?
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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 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…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

706 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

18 Experts available now in Live!

Get 1:1 Help Now