0

I'm new to Oracle DB, coming from MS SQL Server.

I created this script:

DEFINE USER_NAME = &1
DEFINE PASSWORD = &2
DEFINE TABLESPACE = &3

CONNECT &DB_ADMIN/&DB_PWD@//localhost:1521/&DB_NAME

SET VERIFY OFF
SET SERVEROUTPUT ON
SET FEEDBACK OFF

DECLARE PROCEDURE Create_User
  ( userName IN VARCHAR2
  , password IN VARCHAR2
  , tSpace IN VARCHAR2
  ) AS
  dbCount INTEGER := -1;
  createStmt VARCHAR2(500);
BEGIN
  SELECT COUNT(*)
    INTO dbCount
    FROM CDB_USERS u
      INNER JOIN v$pdbs d ON u.CON_ID = d.CON_ID
    WHERE d.NAME = '&DB_NAME'
      AND u.USERNAME = userName;

  IF dbCount > 0
    THEN
      EXECUTE IMMEDIATE 'DROP USER ' || userName || ' CASCADE';
      DBMS_OUTPUT.PUT_LINE('User ' || userName || ' dropped.');
    END IF;

  createStmt := 'CREATE USER ' || userName || ' IDENTIFIED BY ' || password || ' DEFAULT TABLESPACE ' || tSpace || ' QUOTA UNLIMITED ON ' || tSpace;
  DBMS_OUTPUT.PUT_LINE('User about to be created:');
  DBMS_OUTPUT.PUT_LINE(createStmt);
  DBMS_OUTPUT.PUT_LINE('');


  DBMS_OUTPUT.PUT_LINE('Creating user ' || userName || ' ...');
  EXECUTE IMMEDIATE createStmt;
  DBMS_OUTPUT.PUT_LINE('Database ' || userName || ' successfully created.');

  END Create_User;

BEGIN
  Create_User('&USER_NAME', '&PASSWORD', '&TABLESPACE');
  END;
  /

When I run it in SqlPlus, like this:

SQL> DEFINE DB_NAME = 'MYDB'
SQL> DEFINE DB_ADMIN = 'SA'
SQL> DEFINE DB_PWD = 'pwd'
SQL> @/db-install/sql/add-user.sql 'USERNAME' 'pwd' 'TSPACE'

I get this error message:

Connected.
DECLARE PROCEDURE Create_User
*
ERROR at line 1:
DECLARE
*
ERROR at line 1:
ORA-01918: user 'USERNAME' does not exist
ORA-06512: at line 19
ORA-06512: at line 36

Can someone please point me to the right direction? What am I doing wrong?

Your answer is appreciated.


EDIT

I have this exact same code in another script, and it's working fine there:

DEFINE DB_NAME = &1
DEFINE DB_ADMIN = &2
DEFINE DB_PWD = &3

CONNECT &DB_ADMIN/&DB_PWD@//localhost:1521/&DB_NAME

SET VERIFY OFF
SET SERVEROUTPUT ON
SET FEEDBACK OFF

BEGIN
  DBMS_OUTPUT.PUT_LINE('Adding tablespaces to database ...');
  END;
  /


--- Procedure: dropping and creating a given tablespace ---
DECLARE
  dbCount INTEGER;
  createStmt VARCHAR2(500);
  filePath dba_data_files.FILE_NAME%TYPE;
PROCEDURE Create_TS
...

EDIT 2

It doesn't make a difference if I put the variables between DECLARE and PROCEDURE:

DECLARE
  dbCount INTEGER := -1;
  createStmt VARCHAR2(500);
PROCEDURE Create_User
  ( userName IN VARCHAR2
  , password IN VARCHAR2
  , tSpace IN VARCHAR2
  ) AS
BEGIN
Connected.
DECLARE
*
ERROR at line 1:
ORA-01918: user 'USERNAME' does not exist
ORA-06512: at line 19
ORA-06512: at line 36
3
  • What is the full error message? For example I get this: DECLARE PROCEDURE Create_User * ERROR at line 1: ORA-00959: tablespace 'TSPACE' does not exist ORA-06512: at line 29 ORA-06512: at line 35 May 26, 2021 at 9:37
  • Thank you for taking the time, @Balasz. I amended my report with additional error message details now. It's weird: I'm trying to create a user with this procedure, but the error message is claiming that the user doesn't exist. In the code I'm never referring to an existing user.
    – AxD
    May 26, 2021 at 9:43
  • @Balasz, your clever comment brought me to the solution to my problem: The PROCEDURE parameter userName conflicted with the column CDB_USERS.USERNAME, which resulted in a stack overflow. After renaming the PROCEDURE parameter from userName to usrName, everything works fine. See techonthenet.com/oracle/errors/ora06512.php Would you want to provide this as an answer? I'd be happy to accept and upvote.
    – AxD
    May 26, 2021 at 9:57

1 Answer 1

1

And that is a great example why you should never name your PL/SQL variables the same as column names.

set serveroutput on
DECLARE
  PROCEDURE Create_User
  ( userName IN VARCHAR2
  , password IN VARCHAR2
  , tSpace IN VARCHAR2
  ) AS
  dbCount INTEGER := -1;
  createStmt VARCHAR2(500);
BEGIN
  SELECT COUNT(*)
    INTO dbCount
    FROM DBA_USERS u
    WHERE u.USERNAME = userName;
  dbms_output.put_line('User count: ' || dbcount);
end;
begin
  create_user('abc', 'abc', 'abc');
end;
/

User count: 26

The u.USERNAME = userName filter is username = username, which returns all rows where username is not null.

3
  • Thank you, @Balazs, yet, I don't want the procedure to be stored in the database. It's supposed to be a temporary procedure in memory.
    – AxD
    May 26, 2021 at 9:26
  • I've tried to reproduce this example: docs.oracle.com/en/database/oracle/oracle-database/18/lnpls/…
    – AxD
    May 26, 2021 at 9:28
  • "I don't want the procedure to be stored in the database. It's supposed to be a temporary procedure in memory" Then why have a pl/sql procedure at all? Creating a user is a simple one-line command. Why not just a simple sql script, or just create them from a command line in sqlplus or SQL Dev? Also, Id' be wary of your unconditional DROP ... CASCADE. You could some serious damage that way.
    – EdStevens
    May 26, 2021 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.