?
Solved

oracle create table if not exist

Posted on 2014-03-06
7
Medium Priority
?
6,033 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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
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.
Suggested Courses

765 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