Create user from string variables in a PL/SQL block

前端 未结 1 710
孤街浪徒
孤街浪徒 2020-12-07 03:32

I use Oracle XE for the sole purpose of developing PHP applications and version 11g has apparently lost the GUI tool to manage users which 10g used to have so I\'d like to p

相关标签:
1条回答
  • 2020-12-07 04:02

    PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:

    The above error is because of the fact that you are using DDL inside PL/SQL. You cannot do that. You must (ab)use EXECUTE IMMEDIATE to issue DDL statements in PL/SQL.

    For example,

    SQL> DECLARE
      2    my_user     VARCHAR2(30) := 'foo';
      3    my_password VARCHAR2(9)  := '1234';
      4  BEGIN
      5    EXECUTE IMMEDIATE 'CREATE USER '||my_user||' IDENTIFIED BY '||my_password;
      6    EXECUTE IMMEDIATE 'GRANT CREATE SESSION TO '||my_user;
      7  END;
      8  /
    
    PL/SQL procedure successfully completed.
    
    SQL> conn foo/1234@pdborcl
    Connected.
    SQL> SHOW USER
    USER is "FOO"
    

    Quick reference from documentation,

    Executing DDL and SCL Statements in PL/SQL

    Only dynamic SQL can execute the following types of statements within PL/SQL program units:

    • Data definition language (DDL) statements such as CREATE, DROP, GRANT, and REVOKE

    • Session control language (SCL) statements such as ALTER SESSION and SET ROLE

    • The TABLE clause in the SELECT statement

    On a side note,

    Creating users and granting privileges are usually database administration tasks taken care by the DBA. It is not a frequent activity done via PL/SQL program. DBA creates the users and grants the necessary privileges as a one time activity.

    0 讨论(0)
提交回复
热议问题