Skip to Main Content
  • Questions
  • TRIGGER not dropping user in Oracle 11g?

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, mahmoud.

Asked: August 17, 2016 - 5:53 pm UTC

Last updated: August 19, 2016 - 2:41 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi ,
i have database 11g , and i have user X and want to drop this user just once database startup .
so i used this trigger :
----------------------------------------------------------------------------------------------------------

CREATE OR REPLACE TRIGGER SYS.TW_drop_user
AFTER STARTUP ON DATABASE

DECLARE
created_date_schema DATE;
total_days NUMBER;
schema_name VARCHAR2 (100);
open_count number;

BEGIN
SELECT TO_DATE (created, 'dd/mm/rrrr')
INTO created_date_schema
FROM v$database;

SELECT owner
INTO schema_name
FROM dba_tables
WHERE table_name = 'RT_LICENSE' AND ROWNUM <= 1;

SELECT TRUNC (SYSDATE) - TO_DATE (created_date_schema, 'dd/mm/rrrr')
DAYS
INTO total_days
FROM DUAL;


IF total_days > 14
THEN

EXECUTE IMMEDIATE ('alter USER "' || schema_name || '" account lock ');

FOR session IN (SELECT sid, serial#
FROM v$session
WHERE username = schema_name)
LOOP
EXECUTE IMMEDIATE 'alter system disconnect session ''' || session.sid || ',' || session.serial# || ''' immediate';
END LOOP;

LOOP
SELECT COUNT(*)
INTO open_count
FROM v$session WHERE username = schema_name;
EXIT WHEN open_count = 0;
dbms_lock.sleep(0.5);
END LOOP;

EXECUTE IMMEDIATE ('DROP USER "' || schema_name || '" CASCADE ');
END IF;
EXCEPTION
WHEN OTHERS
THEN
NULL;
END;

--------------------------------------------------------------------------------------------------------------------
but nothing happen , i login to sqlplus and connect to this user and still not dropped , shutdown and start database , login with sys i even change trigger to be after log AFTER LOGON.. user not dropped !!
but if i'm running this script alone without trigger , its working fine .
---------------
any help please , thanks in Advance.

and Chris said...

This seems a bizarre requirement. Why on earth do you want to drop a user on startup? Why not remove them normally?

Anyway, my guess is something is failing when the trigger fires. My bet is on

select to_date ( created, 'dd/mm/rrrr' )
into created_date_schema
from v$database


v$database.created is already a date!

If the nls settings for the database aren't dd/mm/rrrr this will raise an exception. This is because it must implicitly convert the date to a string first.

There's no need to to_date() it. Just select into.

But you don't know, because you also have:

exception
  when others then
    null;


This will bury any exceptions you get. At the very least you should log the exception to a table. Ideally you'd re-raise it too.

Do this and you'll see the error. And you'll probably have your answer to why it's not working!

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

mahmoud Yousef, August 18, 2016 - 5:09 pm UTC

Thanks Chris for your answer, acutely i'm planning to give to one of my client in test environment an application and want him to just test the application , and give him the system for 15 days .
so , i just want to be sure that the system will be removed from the test environment after 15 days .
Chris Saxon
August 19, 2016 - 2:41 am UTC

Perhaps an easier option is to use a profile ?

- set password expiry for 15 days
- set no grace time

Then when the password expires, their account will be locked, and they can no longer logon.

But your approach seems optimistic for a client site, because if they have any kind of DBA privs, pretty much anything you can could be over-ridden.

Cheers,
Connor

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library