gpt4 book ai didi

Java PL/SQL 'Execute Immediate'

转载 作者:行者123 更新时间:2023-12-01 21:16:48 28 4
gpt4 key购买 nike

我正在 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

请告知该怎么做。

最佳答案

如果 ? 是绑定(bind)变量,则不能在 create user 语句中使用它们。它需要完整的文字文本。

顺便说一句,您的意思真的是CREATE ANY INDEX吗?这将允许新用户索引整个数据库中的任何表。通常,简单的 CREATE INDEX 就足够了。

最好提前定义一个或多个角色,然后将这些角色授予新用户,而不是硬编码 every possible privilege 。是否应该允许用户创建 View 、过程、类型等?如果它是一个开发者帐户,它是否应该能够调试代码和定义锁?等等...

(只是出于兴趣,你用大写字母编写 Java,还是只用 PL/SQL 编写?)

关于Java PL/SQL 'Execute Immediate',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39889500/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com