dbTalk Databases Forums  

best "drop table if exists" idiom?

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss best "drop table if exists" idiom? in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mh@pixar.com
 
Posts: n/a

Default best "drop table if exists" idiom? - 04-21-2009 , 02:36 PM






I'm converting some mysql scripts... what's the best oracular
idiom for mysql's "drop table if exists" command?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios

Reply With Quote
  #2  
Old   
ddf
 
Posts: n/a

Default Re: best "drop table if exists" idiom? - 04-21-2009 , 03:08 PM






On Apr 21, 2:36*pm, m... (AT) pixar (DOT) com wrote:
Quote:
I'm converting some mysql scripts... what's the best oracular
idiom for mysql's "drop table if exists" command?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios
There isn't one, really. Of course you're free to write PL/SQL to do
the 'dirty deed':

set serveroutput on size 1000000

declare
v_sqltxt varchar2(2000);
v_success number:=0;
v_tabname user_tables.table_name%type:=null;
p_tabname user_tables.table_name%type:='&1';
begin
select table_name
into v_tabname
from user_tables where table_name = upper(p_tabname);
if v_tabname is not null then
v_sqltxt:='drop table '||v_tabname;
execute immediate v_sqltxt;
end if;
exception
when no_data_found then
dbms_output.put_line('Table '||p_tabname||' does not
exist');
end;
/

Personally I don't mind seeing the error 'table or view does not
exist' as it's not usually a script killer.


David Fitzjarrell


Reply With Quote
  #3  
Old   
Mark D Powell
 
Posts: n/a

Default Re: best "drop table if exists" idiom? - 04-22-2009 , 08:00 AM



On Apr 21, 4:08*pm, ddf <orat... (AT) msn (DOT) com> wrote:
Quote:
On Apr 21, 2:36*pm, m... (AT) pixar (DOT) com wrote:

I'm converting some mysql scripts... what's the best oracular
idiom for mysql's "drop table if exists" command?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios

There isn't one, really. *Of course you're free to write PL/SQL to do
the 'dirty deed':

set serveroutput on size 1000000

declare
* * * * * v_sqltxt varchar2(2000);
* * * * * v_success number:=0;
* * * * * v_tabname user_tables.table_name%type:=null;
* * * * * p_tabname user_tables.table_name%type:='&1';
begin
* * * * * select table_name
* * * * * into v_tabname
* * * * * from user_tables where table_name = upper(p_tabname);
* * * * * if v_tabname is not null then
* * * * * * * *v_sqltxt:='drop table '||v_tabname;
* * * * * * * *execute immediate v_sqltxt;
* * * * * end if;
exception
* * * * * when no_data_found then
* * * * * * * * *dbms_output.put_line('Table '||p_tabname||' does not
exist');
end;
/

Personally I don't mind seeing the error 'table or view does not
exist' as it's not usually a script killer.

David Fitzjarrell
First of all you should consider if you need to be dropping and
creating tables as part of the process under conversion. Perhaps the
tables in question should be replaced with Oracle temporary tables
which are defined once and then only exist within a session once
populated by an insert for use in a later select.

Otherwise, just code the drop and ignore errors OR perhaps remove the
drop and creates and replace the create with a truncate. The tables
will just be cleaned out prior to each use.

The temporary table method is best when multiple user sessions might
want to run concurrently as otherwise to use a common table you would
need to single thread the user sessions. Also only the owner or
especially privileged users can truncate a table natively so to use
truncate on non-owned tables you can write a procedure under the table
owner that performs the truncate command via 'execute immediate' (PL/
SQL statement).

HTH -- Mark D Powell --


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.