Solved

How can check if the table allready exist

Posted on 2008-06-24
8
5,313 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 142

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

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.

Question has a verified solution.

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

Suggested Solutions

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
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…
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 syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

776 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