Solved

How can check if the table allready exist

Posted on 2008-06-24
8
5,314 Views
Last Modified: 2010-04-21
EXECUTE IMMEDIATE'TRUNCATE TABLE TEMP_AUTORACK_FLAG';  
EXECUTE IMMEDIATE'DROP TABLE TEMP_AUTORACK_FLAG';      
 
EXECUTE IMMEDIATE'CREATE GLOBAL TEMPORARY TABLE TEMP_AUTORACK_FLAG
(AUTORACK_FLAG CHAR(1)) ON COMMIT PRESERVE ROWS';

EXECUTE IMMEDIATE'INSERT INTO TEMP_AUTORACK_FLAG
SELECT DISTINCT EQUIP_DATA_VALUE FROM WRK_EQUIPMENT_SUPLM_TMP
where EQUIP_DATA_ID = ARCG';  

I am running this code , table does not exist when i encounter the first statement. How can i check if table exist then do the truncate.
0
Comment
Question by:midwestexp
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21855406
why do you tuncate, then drop and finally recreate a globally temporary table.
that is "nonsense".

create the global temp table once, and use it, and don't use the execute immediate for it.
0
 
LVL 17

Assisted Solution

by:ram_0218
ram_0218 earned 100 total points
ID: 21855570
i agree what you'r trying to do does not make sense at all! but if you want to definitely check if the table exists then:

declare a variable in your declare block like this:
v_count NUMBER:=0;

and then in your body, find out if that table exists in all_tables table.

select count(*) into v_count
from all_tables where table_name = 'TEMP_AUTORACK_FLAG';

if v_count > 1 THEN
 --that means table exists.. here do truncate or delete whatever
else
 --table doesnt exist
end if;
0
 
LVL 22

Accepted Solution

by:
DrSQL earned 400 total points
ID: 21855680
midwestexp,
   Another way to handle this is to use the exception handler as a way of deciding if you want to truncate the table or create a new one:

.
.
.
begin
   EXECUTE IMMEDIATE 'TRUNCATE TABLE TEMP_AUTORACK_FLAG';
 exception when others then
             EXECUTE IMMEDIATE 'DROP TABLE TEMP_AUTORACK_FLAG';      
 
             EXECUTE IMMEDIATE'CREATE GLOBAL TEMPORARY TABLE TEMP_AUTORACK_FLAG
             (AUTORACK_FLAG CHAR(1)) ON COMMIT PRESERVE ROWS';
end;

EXECUTE IMMEDIATE'INSERT INTO TEMP_AUTORACK_FLAG
SELECT DISTINCT EQUIP_DATA_VALUE FROM WRK_EQUIPMENT_SUPLM_TMP
where EQUIP_DATA_ID = ARCG';


Good luck!
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 21858656
I agree with angellll and ram_0218: this looks like a *VERY* poor approach to a problem!  Or, it looks something like a SQL Server way of solving an Oracle problem.

What is the business reason for this approach?  Or, why can you not simply create the global temporary table once (outside of PL\SQL) then just use it when you need it.  That's how global temporary tables work best!
0
 
LVL 6

Expert Comment

by:divyeshhdoshi
ID: 21863605
Hi,

select count(*) from user_tables where table_name = upper(tablename);
0
 
LVL 3

Expert Comment

by:gajmp
ID: 21863721
Instead of that you can do like below
/*execute this script sepratly...(one time execution) Whenever we defining the global temp table data will be exists till the session end (if we use ON COMMIT PRESERVE ROWS) or still the commit (if we use ON COMMIT DELETE ROWS). So no need to truncate and drop everytime */
CREATE GLOBAL TEMPORARY TABLE TEMP_AUTORACK_FLAG (
  EQUIP_DATA_VALUE char(1)
) ON COMMIT PRESERVE ROWS;

/* write the below script in procedure and execute the procedure whenever u want or execute the script in in SQL*plus whenever u want*/
EXECUTE IMMEDIATE'INSERT INTO TEMP_AUTORACK_FLAG
SELECT DISTINCT EQUIP_DATA_VALUE FROM WRK_EQUIPMENT_SUPLM_TMP
where EQUIP_DATA_ID = ARCG';  
0
 

Author Closing Comment

by:midwestexp
ID: 31470123
I am New to Oracle , Thank you Guys.
0
 
LVL 22

Expert Comment

by:DrSQL
ID: 21865442
midwestexp,
   Just FYI - I really should NOT have included the DROP TABLE.  If the truncate fails, you can assume that the table needs to be created - although checking for the specific error would be better.  that would be:
.
.
begin
   EXECUTE IMMEDIATE 'TRUNCATE TABLE TEMP_AUTORACK_FLAG';
 exception when others then
             EXECUTE IMMEDIATE'CREATE GLOBAL TEMPORARY TABLE TEMP_AUTORACK_FLAG
             (AUTORACK_FLAG CHAR(1)) ON COMMIT PRESERVE ROWS';
end;

EXECUTE IMMEDIATE'INSERT INTO TEMP_AUTORACK_FLAG
SELECT DISTINCT EQUIP_DATA_VALUE FROM WRK_EQUIPMENT_SUPLM_TMP
where EQUIP_DATA_ID = ARCG';

Good luck!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that useā€¦
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

821 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