Friday, February 5, 2010

Create list of oracle users/schema's via PLSQL by reading list from a file.

CREATE DIRECTORY DUMP_MDSP_DIR AS '/u01/oracle/work';
GRANT READ,WRITE ON DUMP_MDSP_DIR TO PUBLIC;

--Now create a file called users.lst and list all the username in it. Save it in /u01/oracle/work.

CREATE OR REPLACE PROCEDURE CREATEUSERS
IS
  fHandler UTL_FILE.FILE_TYPE;
  buf      varchar2(100);
BEGIN
  fHandler:= UTL_FILE.FOPEN('DUMP_MDSP_DIR', 'users.lst', 'r');
  loop
  BEGIN

UTL_FILE.GET_LINE(fHandler, buf);
EXECUTE IMMEDIATE 'create user '||buf||' '||' identified by admin default tablespace users001 temporary tablespace temp001';
EXECUTE IMMEDIATE 'grant connect,resource,create session to '||buf;

  EXCEPTION
     WHEN NO_DATA_FOUND THEN EXIT;
  END;
  end loop;
  UTL_FILE.FCLOSE(fHandler);
EXCEPTION
  WHEN utl_file.invalid_path THEN
     raise_application_error(-20000, 'Invalid path. Create directory or set UTL_FILE_DIR.');
END;
/