cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

drop table in execute immediate

Baron
Participant
2,971

I am trying to build a cursor on systable to find all old tables, and then exceute drop table statement on all those tables:

declare tnames insensitive cursor for select table_name from systable where table_name like '%_old';

and then within the loop I execute the drop statement:

execute immediate ('drop table ' + tname) ;

I rceive an error stating that the cursor is not open!! Is this generally possible? Is there any other alternative?

View Entire Topic
chris_keating
Product and Topic Expert
Product and Topic Expert

DROP TABLE has an implicit commit which closes the cursor . You will need to OPEN the cursor WITH HOLD .

Baron
Participant
0 Likes

One more question, does the same work for dropping triggers?

declare tnames insensitive cursor for select trigname from systriggers;

execute immediate ('drop trigger ' + tname) ;

I receive a syntax error!!

VolkerBarth
Contributor
0 Likes

Do you supply the trigger name or the table name to the DROP TRIGGER statement? The trigger name is mandatory.

Are the trigger names unique? Otherwise, you need to specify the table (and possibly owner) name, too.

Baron
Participant
0 Likes

Sorry for the inconvinience, the problem was that one of the triggers was including ',' in its name (in trigname).

Problem was solved after adding "" to the table name:

execute immediate ('drop trigger "' + tname + '"')