从 PL/SQL 块中的字符串变量创建用户
Create user from string variables in a PL/SQL block
我使用 Oracle XE 的唯一目的是开发 PHP 应用程序,而 11g 版显然已经失去了 10g 曾经拥有的用于管理用户的 GUI 工具,所以我想准备一个代码片段来创建来自命令行的用户。我正在尝试 define variables,这样我就不需要输入相同的用户名 16 次,但语法不正确:
DECLARE
my_user VARCHAR2(30) := 'foo';
my_password VARCHAR2(9) := '1234';
BEGIN
CREATE USER my_user IDENTIFIED BY my_password DEFAULT TABLESPACE users;
GRANT CONNECT, RESOURCE TO my_user;
GRANT CREATE DATABASE LINK TO my_user;
GRANT CREATE MATERIALIZED VIEW TO my_user;
GRANT CREATE PROCEDURE TO my_user;
GRANT CREATE PUBLIC SYNONYM TO my_user;
GRANT CREATE ROLE TO my_user;
GRANT CREATE SEQUENCE TO my_user;
GRANT CREATE SYNONYM TO my_user;
GRANT CREATE TABLE TO my_user;
GRANT CREATE TRIGGER TO my_user;
GRANT CREATE TYPE TO my_user;
GRANT CREATE VIEW TO my_user;
GRANT SELECT_CATALOG_ROLE TO my_user;
GRANT SELECT ANY DICTIONARY TO my_user;
END;
/
CREATE USER my_user IDENTIFIED BY my_password DEFAULT TABLESPACE users;
*
ERROR en línea 5:
ORA-06550: line 5, column 2:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
是否禁止在 PL/SQL 块中使用 CREATE USER
语句,或者我只是犯了一个愚蠢的错字?是否必须使用 SQL*Plus 变量?
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
上面的错误是因为你在PL/SQL里面使用了DDL。你不能这样做。您必须(绝对)使用 EXECUTE IMMEDIATE 在 PL/SQL 中发出 DDL 语句。 =22=]
例如,
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"
来自 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
旁注,
创建用户和授予权限通常是 数据库管理 任务,由 DBA 负责。它不是通过 PL/SQL 程序频繁完成的 activity。 DBA 一次性创建用户并授予必要的权限 activity.
我使用 Oracle XE 的唯一目的是开发 PHP 应用程序,而 11g 版显然已经失去了 10g 曾经拥有的用于管理用户的 GUI 工具,所以我想准备一个代码片段来创建来自命令行的用户。我正在尝试 define variables,这样我就不需要输入相同的用户名 16 次,但语法不正确:
DECLARE
my_user VARCHAR2(30) := 'foo';
my_password VARCHAR2(9) := '1234';
BEGIN
CREATE USER my_user IDENTIFIED BY my_password DEFAULT TABLESPACE users;
GRANT CONNECT, RESOURCE TO my_user;
GRANT CREATE DATABASE LINK TO my_user;
GRANT CREATE MATERIALIZED VIEW TO my_user;
GRANT CREATE PROCEDURE TO my_user;
GRANT CREATE PUBLIC SYNONYM TO my_user;
GRANT CREATE ROLE TO my_user;
GRANT CREATE SEQUENCE TO my_user;
GRANT CREATE SYNONYM TO my_user;
GRANT CREATE TABLE TO my_user;
GRANT CREATE TRIGGER TO my_user;
GRANT CREATE TYPE TO my_user;
GRANT CREATE VIEW TO my_user;
GRANT SELECT_CATALOG_ROLE TO my_user;
GRANT SELECT ANY DICTIONARY TO my_user;
END;
/
CREATE USER my_user IDENTIFIED BY my_password DEFAULT TABLESPACE users; * ERROR en línea 5: ORA-06550: line 5, column 2: PLS-00103: Encountered the symbol "CREATE" when expecting one of the following: ( begin case declare exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge
是否禁止在 PL/SQL 块中使用 CREATE USER
语句,或者我只是犯了一个愚蠢的错字?是否必须使用 SQL*Plus 变量?
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
上面的错误是因为你在PL/SQL里面使用了DDL。你不能这样做。您必须(绝对)使用 EXECUTE IMMEDIATE 在 PL/SQL 中发出 DDL 语句。 =22=]
例如,
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"
来自 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
, andREVOKE
Session control language (SCL) statements such as
ALTER SESSION
andSET ROLE
- The
TABLE
clause in theSELECT
statement
旁注,
创建用户和授予权限通常是 数据库管理 任务,由 DBA 负责。它不是通过 PL/SQL 程序频繁完成的 activity。 DBA 一次性创建用户并授予必要的权限 activity.