Solved

oracle create table if not exist

Posted on 2014-03-06
7
5,777 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
[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
  • 4
  • 2
7 Comments
 
LVL 77

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 77

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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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 77

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 77

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

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.  …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

724 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