Solved

oracle create table if not exist

Posted on 2014-03-06
7
5,426 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query 8 50
sql help 8 55
Need a SQL query that creates a header row and one or more detail rows. 7 34
TSQL query to generate xml 4 36
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
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 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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

825 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