Solved

How can check if the table allready exist

Posted on 2008-06-24
8
5,311 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
 
LVL 34

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

Join & Write a Comment

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now