0

I want to create users in oracle, using my existing table call EMPLOYEE, I want to generate a query to create users with user name. User name is in EMPLOYEE table.

I want to create a query like below

CREATE USER (SELECT first_name FROM employee where employee_id = '1') 
IDENTIFIED BY password .....so on
3
  • Mind expanding this with more details? Do you have a User table your are trying to populate from the Employee table? What SQL have you tried? Commented Dec 19, 2014 at 16:02
  • Yes. EMPLOYEE table. it includes first_name Commented Dec 19, 2014 at 16:07
  • I have tried the above code Commented Dec 19, 2014 at 16:08

1 Answer 1

4

DBMS_UTILITY.EXEC_DDL_STATEMENT can be used to run DDL statements in PLSQL so you can try the below

BEGIN
FOR c IN (select 'CREATE USER  ' || first_name || ' ' || 'IDENTIFIED BY ' ||
                 password_column ||' ' as rec
            from employee where employee_id=1) LOOP
  begin
    dbms_utility.exec_ddl_statement(c.rec);
  exception
    when others then
      dbms_output.enable;
dbms_output.put_line  ('failed for '||c.rec);
dbms_output.put_line(sqlerrm);
    end;
  END LOOP;
  END;
Sign up to request clarification or add additional context in comments.

3 Comments

this will create user based on first_name from Employee Table
@ArunaDissanayake, hi. Your problem is that you can't use subqueries in CREATE USER statements. The alternative is the one proposed by psaraj12 that consists in dynamically creating and executing as many CREATE USER statement as you have users in your table. However for the sake of safety I would rather export them to the console than execute them on the fly, at least until you know the code works as you intend.
Good answer, but i recommend you do not use when other then null, because you are hiding possible errors that can occurs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.