Oracle - 查询内部查询

时间:2014-12-19 15:59:18

标签: sql oracle

我想在oracle中创建用户,使用我现有的表调用EMPLOYEE,我想生成一个查询来创建具有用户名的用户。用户名在EMPLOYEE表中。

我想创建一个如下所示的查询

CREATE USER (SELECT first_name FROM employee where employee_id = '1') 
IDENTIFIED BY password .....so on

1 个答案:

答案 0 :(得分:4)

DBMS_UTILITY.EXEC_DDL_STATEMENT可用于在PLSQL中运行DDL语句,因此您可以尝试以下

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;