• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5320
  • Last Modified:

How can check if the table allready exist

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
midwestexp
Asked:
midwestexp
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
ram_0218Commented:
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
 
DrSQLCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Mark GeerlingsDatabase AdministratorCommented:
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
 
divyeshhdoshiCommented:
Hi,

select count(*) from user_tables where table_name = upper(tablename);
0
 
gajmpCommented:
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
 
midwestexpAuthor Commented:
I am New to Oracle , Thank you Guys.
0
 
DrSQLCommented:
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now