Error: DPI-1059: bind variables are not supported in DDL statements when call procedure using node-oracledb

Error: DPI-1059: bind variables are not supported in DDL statements when call procedure using node-oracledb

我在 sql-developer 中创建了以下存储过程:

CREATE OR REPLACE PROCEDURE CREATE_USER(
 t_username IN VARCHAR2, 
 t_password IN VARCHAR2,
 t_default_table IN VARCHAR2,
 t_quota IN VARCHAR2
)
IS
    m_statement VARCHAR2(1300);
    
    m_username VARCHAR2(30) := t_username;
    m_password VARCHAR2(30) := t_password;
    m_default_table VARCHAR2(30) := t_default_table;
    m_quota VARCHAR2(30) := t_quota;
    
BEGIN
    m_statement := 'create user ' || t_username || ' identified by ' || t_password;

    IF m_default_table != 'NULL' THEN
         m_statement := m_statement || ' DEFAULT TABLESPACE ' || m_default_table;
    END IF;

    IF m_quota != '0' AND m_default_table != 'NULL' THEN
        m_statement := m_statement || ' QUOTA ' || m_quota || 'M ON ' || m_default_table;
    END IF;
    EXECUTE IMMEDIATE (m_statement);
END;

而且编译没有错误。我也连接到甲骨文。然后我得到了这样的用户数据(req.body):

{ username: 'a', password: 'a', tablespace: 'NULL', quota: '0' }

但是当我执行 'call' 过程查询时:

oracle.getConnection(
        {
            uuser : "AN",
            password: "AN123",
            connectString: "localhost:1521/orcl"
        },
        (t_err, t_connection) => {
            if(t_err){
                console.error(t_err);
                return;
            }
            t_connection.execute(
                `BEGIN 
                    createUser(:username, :password, :tablespace, :quota);
                END;`,
                {
                    username: req.body.username,
                    password: req.body.password,
                    tablespace: req.body.tablespace,
                    quota: req.body.quota,
                    
                },
                (t_er, t_reslt) => {
                    if(t_er){
                        console.error(t_er);
                        return;
                    }

我收到了:

[Error: DPI-1059: bind variables are not supported in DDL statements] {
  errorNum: 0,
  offset: 0
}

我尝试了很多方法来修复程序或 nodejs 代码,但都没有用。我对这个话题很陌生。谁能帮帮我吗?

错误代码 DPI-1059 看起来很陌生,不像是 Oracle 错误消息。我检查过 node-oracledb, they use NJS-000 to NJS-080. Googling lead to Oracle's Database Programming Interface for C, which defines this error code in dpiErrorMessages.h:

"DPI-1059: bind variables are not supported in DDL statements", // DPI_ERR_NO_BIND_VARS_IN_DDL

常量DPI_ERR_NO_BIND_VARS_IN_DDL在源代码中只使用了一次,在文件dpiStmt.c:

// attempt to improve message "ORA-01036: illegal variable name/number"
if (status < 0) {
    if (error->buffer->code == 1036) {
        if (stmt->statementType == DPI_STMT_TYPE_CREATE ||
                stmt->statementType == DPI_STMT_TYPE_DROP ||
                stmt->statementType == DPI_STMT_TYPE_ALTER)
            dpiError__set(error, error->buffer->action,
                    DPI_ERR_NO_BIND_VARS_IN_DDL);
    }
    return DPI_FAILURE;
}

由于 Oracle 的错误消息非常混乱,值得研究 ORA-01036,例如 here

你的代码对我来说运行没有错误 - 一旦我解决了 SQL 语句 CREATE_USER 中的名称与 JS 文件 createUser 中的名称之间的冲突。确实确保您没有调用 PL/SQL 包的某些先前版本,这些包确实尝试在 CREATE USER 语句中使用绑定变量(这在评论中有说明)。如错误所述:Oracle DB 不支持此用法。例如,你不能做:

connection.execute(`create user :un identified by :pw`, ['cj2', 'cj2password']);
  • 这是一个 DDL 语句
  • 它尝试使用绑定变量

这不受支持。它因 DPI-1059 而失败。绑定变量用于数据,而不是语句文本。

另外两个提示:

  • 过滤或清理您的输入值以避免SQL注入攻击
  • 使用 async/await 编程风格(而不是回调)来避免一些麻烦。查看所有 node-oracledb examples.