on 2019 Mar 07 7:21 AM
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?
Request clarification before answering.
DROP TABLE has an implicit commit which closes the cursor . You will need to OPEN the cursor WITH HOLD .
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
See that old question on cursor loops and statements within the loop that do an automatic commit (like DROP TABLE does).
Basically, you have to make sure the cursor is hold open, for details see Bruce's answer.
To add: You could also adapt the "close_on_endtrans" option but I would not recommend that.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
> the "close_on_endtrans" option but I would not recommend that.
Why not? The FOR loop works just like close_on_endtrans = off, and FOR is often an excellent alternative to DECLARE OPEN FETCH.
In fact, I cannot recall a single time when implicitly closing a cursor on COMMIT or ROLLBACK was the desired behavior.
Well, because the option will affect all transactions (unless set temporarily or only connection-wise) and all cursors, and I guess most cursors are used "behind the curtain" in database applications, and I would be hesitant to possibly modify their behaviour.
Therefore, I strongly suggest to use a local cursor WITH HOLD or - even better - the great FOR loop. (I almost always use cursor loops with FOR, I only do not use them when the implicit WITH HOLD is not desired:...)
User | Count |
---|---|
54 | |
6 | |
6 | |
5 | |
5 | |
5 | |
3 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.