Wednesday, December 23, 2015

How to reset password in Oracle after the current password has expired

declare
 cursor pass is 
SELECT    
     'ALTER '||
     SUBSTR(DBMS_LOB.SUBSTR(DBMS_METADATA.GET_DDL('USER',USERNAME),
     INSTR(DBMS_LOB.SUBSTR(DBMS_METADATA.GET_DDL('USER',USERNAME),200),'DEFAULT')-1) ,
     INSTR(DBMS_LOB.SUBSTR(DBMS_METADATA.GET_DDL('USER',USERNAME),200),'USER')) USER_PASS 
FROM  DBA_USERS 
WHERE USERNAME IN 
     (SELECT USERNAME  
      FROM DBA_USERS 
      WHERE (USERNAME  in
            (select username  from dba_users where account_status like '%EXPIRED%' and
              (
               username like 'SYSTEM' or 
               username like 'TEST%' ))));

BEGIN
  for rec in pass loop
   begin
    execute immediate rec.USER_PASS;
    exception when others then DBMS_OUTPUT.PUT_LINE(SQLERRM);
   end;
  end loop;
END;
/