I have the following PL/SQL being sent to a remote oracle 11gr2 server via ADO.
It’s purpose is to check whether a user exists. Then if it does, kill of all its connections. Finally it drops the user.
DECLARE
i INTEGER;
BEGIN
select count(1) into i from dba_users where username='<schema>';
if i=0 THEN
FOR c IN (SELECT s.sid,s.serial# FROM v$session s WHERE s.username = '<schema>') LOOP
EXECUTE IMMEDIATE 'alter system kill session ''' ||c.sid || ',' || c.serial# || '''';
END LOOP;
drop user <schema> Cascade;
END IF;
END;
The error message I have received after much tweaking is still:
ERROR:[Microsoft][ODBC driver for Oracle][Oracle]ORA-06550: line 1,
column 286: PLS-00103: Encountered the symbol “DROP” when expecting
one of the following:( begin case declare else elsif end exit for goto if loop mod
null pragma raise return select update while with
<<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
It doesn’t like the syntax of having the drop inside of the IF statement. Does anyone know who to make this run properly?
EDIT:
To be clear, I would normally never execute this statement in this manner. But due to the environment, it is the only possible way and is not creating any security risks. I know I’m violating nearly every good practice, but it is necessary this time!
You cannot issue DDL (i.e. DROP) statements directly in PL/SQL you’ll need to run your DROP statement using dynamic SQL.
The easiest way to achieve this is with the
EXECUTE IMMEDIATEstatement (in a similar way to how you have already used it for theALTER SESSIONcommand):http://download.oracle.com/docs/cd/B12037_01/appdev.101/b10807/13_elems017.htm
Incidentally, you might want to look into using bind variables instead of concatenating the values in your dynamic SQL as it improves performance, especially in a loop.
e.g.
Hope it helps…