Se non esco una tabella e la tabella non esiste, ricevo un errore
Domanda
Devo lasciare un tavolo e crearne uno nuovo. Se non esco la tabella e la tabella non esiste, ricevo un errore
Come posso verificare se esiste la tabella?
Sto lavorando su Oracle 11g
Grazie in anticipo.
Soluzione
Potresti fare qualcosa del genere:
DECLARE v_exist PLS_INTEGER;
BEGIN
SELECT COUNT(*) INTO v_exist
FROM user_tables
WHERE table_name = 'YOURTABLEHERE';
IF v_exist = 1 THEN
EXECUTE IMMEDIATE 'DROP TABLE YOURTABLEHERE';
END IF;
Altri suggerimenti
DECLARE
eTABLE_OR_VIEW_DOES_NOT_EXIST EXCEPTION;
PRAGMA EXCEPTION_INIT(eTABLE_OR_VIEW_DOES_NOT_EXIST, -942);
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE SCHEMA.WHATEVER';
EXCEPTION
WHEN eTABLE_OR_VIEW_DOES_NOT_EXIST THEN
NULL;
END;
Condividi e divertiti.
qualcosa di simile a
select count(*) from user_tables
where table_name= :table name
o
select count(*) from dba_tables
where owner = :table owner
and table_name = :table name
o un'alternativa a mano pesante:
begin execute immediate 'drop table table_name';
exception when others then null;
end;
Ho usato la seguente procedura per prendermi cura di questo:
create or replace procedure drop_table_if_exists ( p_table_name varchar2 )
is
it_exist number;
begin
select count(*)
into it_exists
from user_tables
where table_name = p_table_name
;
if it_exists >= 1 then
execute immediate 'drop table '||p_table_name;
end if;
end;
/
exec drop_table_if_exists ( 'TABLE_TO_DROP' );
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow