我在Oracle数据库上编写Java应用程序。我正在使用的PL / SQL语句是:
DECLARE
USER_COUNT INTEGER;
BEGIN
SELECT COUNT(*) INTO USER_COUNT FROM dba_users WHERE username=?;
IF (USER_COUNT = 0) THEN
EXECUTE IMMEDIATE 'CREATE USER ? IDENTIFIED BY ?';
EXECUTE IMMEDIATE 'GRANT CREATE SESSION, CREATE TABLE, CREATE ANY INDEX TO ?';
ELSE
raise_application_error(-20101, 'User ? already exists. Please drop it or choose another username.');
END IF;
END;
/
但如果问号周围有引号,我会收到很多'无效列索引'错误。例如:
EXECUTE IMMEDIATE 'CREATE USER ? IDENTIFIED BY ?';
无效,但
EXECUTE IMMEDIATE CREATE USER ? IDENTIFIED BY ?;
很好。
但是,如果我选择使用第二种形式,则会出现另一种语法错误:
java.sql.SQLException: ORA-06550: line 6, column 23:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
( - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
continue avg count current exists max min prior sql stddev
sum variance execute forall merge time timestamp interval
date <a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an alternat
请告知该怎么做。
答案 0 :(得分:2)
如果?
是绑定变量,则无法在create user
语句中使用这些变量。它需要完整的文字文本。
CREATE ANY INDEX
吗?这将让新用户索引整个数据库中的任何表。通常普通CREATE INDEX
就足够了。
最好事先定义一个或多个角色,然后将这些角色授予新用户,而不是硬编码every possible privilege。是否应该允许用户创建视图,过程,类型等?如果它将成为开发者帐户,它是否应该能够调试代码并定义锁定?等等...
(只是出于兴趣,您是以大写形式编写Java,还是只编写PL / SQL?)
答案 1 :(得分:1)
试试这个。
DECLARE
USER_COUNT INTEGER;
BEGIN
SELECT COUNT (*)
INTO USER_COUNT
FROM dba_users
WHERE username =?;
IF USER_COUNT = 0 THEN
EXECUTE IMMEDIATE 'CREATE USER '||?|| 'IDENTIFIED BY '||?;
EXECUTE IMMEDIATE 'GRANT CREATE SESSION, CREATE TABLE, CREATE ANY INDEX TO '|| ?;
ELSE
raise_application_error(-20101, 'User ? already exists. Please drop it or choose another username.');
END IF;
END;
/
答案 2 :(得分:0)
在EXECUTE IMMEDIATE中使用DDL时,不能使用绑定变量。
您可以参考下面的@链接,可以获得更好的解释。