[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

oracle create table if not exist

Posted on 2014-03-06
7
Medium Priority
?
6,333 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

656 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