Solved

How can check if the table allready exist

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle rollup query 3 50
EXECUTE IMMEDIATE 5 51
automatic email alert 1 41
Query to identify changes between rows of two tables 8 37
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.  …
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.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to recover a database from a user managed backup

930 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

10 Experts available now in Live!

Get 1:1 Help Now