节点oracledb执行pl/sql函数
Node oracledb execute pl/sql function
我创建了如下内容:
DECLARE
active_ver number;
archived_ver number;
draft_ver number;
ret varchar2(10);
FUNCTION checkStatus(active_ver IN number, archived_ver IN number, draft_ver
IN number)
return varchar2 is varchar2(10);
BEGIN
IF (archived_ver > active_ver) then
IF (draft_ver > archived_ver) then
z:='DRAFT';
ELSE z:='ARCHIVED';
END IF;
ELSE z:='ACTIVE';
END IF;
RETURN z;
END;
BEGIN
SELECT NVL(max(version),0) into active_ver from table_1 order by run_id desc;// returns 0
SELECT NVL(max(version),0) into archived_ver from table_2 order by run_id desc;// returns 16
SELECT NVL(max(version),0) into draft_ver from table_3 order by run_id desc;// returns 11.6
ret = checkStatus(active_ver, archived_ver, draft_ver);
dbms_output.put_line('ret' || ret);
END;
它运行良好,当我在 SQL Developer 中执行它时 returns 已存档,但是当我尝试使用节点 oracledb 执行此操作时,我得到了 ACTIVE :(
我就是这样做的:
async executeFunction(){
const query = 'DECLARE
active_ver number;
archived_ver number;
draft_ver number;
ret varchar2(10);
FUNCTION checkStatus(active_ver IN number, archived_ver IN number, draft_ver IN number)
return varchar2 is varchar2(10);
BEGIN
IF (archived_ver > active_ver) then
IF (draft_ver > archived_ver) then
z:='DRAFT';
ELSE z:='ARCHIVED';
END IF;
ELSE z:='ACTIVE';
END IF;
RETURN z;
END;
BEGIN
SELECT NVL(max(version),0) into :active_ver from table_1 order by run_id desc;// returns 0
SELECT NVL(max(version),0) into :archived_ver from table_2 order by run_id desc;// returns 16
SELECT NVL(max(version),0) into :draft_ver from table_3 order by run_id desc;// returns 11.6
:ret = checkStatus(active_ver, archived_ver, draft_ver);
dbms_output.put_line('ret' || ret);
END;'
const result = connection.execute(query,
{
active_ver : {dir: oracledb.BIND_OUT,type: oracledb.NUMBER}.
archived_ver : {dir: oracledb.BIND_OUT,type: oracledb.NUMBER}.
draft_ver : {dir: oracledb.BIND_OUT,type: oracledb.NUMBER}.
ret : {dir: oracledb.BIND_OUT,type: oracledb.STRING}.
});
console.log('max ACTIVE ver: ' + result.outBinds.active_ver);
console.log('max ARCHIVED ver: ' + result.outBinds.archived_ver);
console.log('max DRAFT ver: ' + result.outBinds.draft_ver);
console.log('Output: ' + result.outBinds.ret);
return result.outBinds.ret;
}
在输出中我得到:
max ACTIVE ver: 0
max ARCHIVED ver: 22
max DRAFT ver: 11.01
Output: ACTIVE
我的问题是为什么我在 sql developer 中得到了一个正确的值,它是 ARCHIVED 但错误的输出是由 oracle db 生成的 ACTIVE???
你能帮忙吗?
在您的 checkStatus() 函数中,参数称为 x、xx 和 xxx,但在函数内部您使用 archived_ver、active_ver 和 draft_ver。
我建议您参考函数内的参数名称,现在是 x、xx 和 xxx(但是您可能想提供更有意义的名称。:) )
从这样的事情开始:
'use strict';
const oracledb = require('oracledb');
let config = require('./dbconfig.js');
async function executeFunction() {
let connection;
const fn =
`CREATE OR REPLACE FUNCTION checkStatus(p_archived_ver in number, p_active_ver in number, p_draft_ver in number) return varchar2 is
z varchar2(20);
BEGIN
IF (p_archived_ver > p_active_ver) then
IF (p_draft_ver > p_archived_ver) then
z := 'DRAFT';
ELSE
z := 'ARCHIVED';
END IF;
ELSE
z := 'ACTIVE';
END IF;
RETURN z;
END;`;
const query =
`DECLARE
active_ver number;
archived_ver number;
draft_ver number;
ret varchar2(10);
BEGIN
SELECT 0 into active_ver from dual; -- returns 0
SELECT 16 into archived_ver from dual; -- returns 16
SELECT 11.6 into draft_ver from dual; -- returns 11.6
:ret := checkStatus(active_ver, archived_ver, draft_ver);
END;`;
try {
connection = await oracledb.getConnection(config);
await connection.execute(fn);
const result = await connection.execute(
query,
{ ret: {dir: oracledb.BIND_OUT,type: oracledb.STRING} });
console.log('Output: ' + result.outBinds.ret);
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
executeFunction();
我创建了如下内容:
DECLARE
active_ver number;
archived_ver number;
draft_ver number;
ret varchar2(10);
FUNCTION checkStatus(active_ver IN number, archived_ver IN number, draft_ver
IN number)
return varchar2 is varchar2(10);
BEGIN
IF (archived_ver > active_ver) then
IF (draft_ver > archived_ver) then
z:='DRAFT';
ELSE z:='ARCHIVED';
END IF;
ELSE z:='ACTIVE';
END IF;
RETURN z;
END;
BEGIN
SELECT NVL(max(version),0) into active_ver from table_1 order by run_id desc;// returns 0
SELECT NVL(max(version),0) into archived_ver from table_2 order by run_id desc;// returns 16
SELECT NVL(max(version),0) into draft_ver from table_3 order by run_id desc;// returns 11.6
ret = checkStatus(active_ver, archived_ver, draft_ver);
dbms_output.put_line('ret' || ret);
END;
它运行良好,当我在 SQL Developer 中执行它时 returns 已存档,但是当我尝试使用节点 oracledb 执行此操作时,我得到了 ACTIVE :(
我就是这样做的:
async executeFunction(){
const query = 'DECLARE
active_ver number;
archived_ver number;
draft_ver number;
ret varchar2(10);
FUNCTION checkStatus(active_ver IN number, archived_ver IN number, draft_ver IN number)
return varchar2 is varchar2(10);
BEGIN
IF (archived_ver > active_ver) then
IF (draft_ver > archived_ver) then
z:='DRAFT';
ELSE z:='ARCHIVED';
END IF;
ELSE z:='ACTIVE';
END IF;
RETURN z;
END;
BEGIN
SELECT NVL(max(version),0) into :active_ver from table_1 order by run_id desc;// returns 0
SELECT NVL(max(version),0) into :archived_ver from table_2 order by run_id desc;// returns 16
SELECT NVL(max(version),0) into :draft_ver from table_3 order by run_id desc;// returns 11.6
:ret = checkStatus(active_ver, archived_ver, draft_ver);
dbms_output.put_line('ret' || ret);
END;'
const result = connection.execute(query,
{
active_ver : {dir: oracledb.BIND_OUT,type: oracledb.NUMBER}.
archived_ver : {dir: oracledb.BIND_OUT,type: oracledb.NUMBER}.
draft_ver : {dir: oracledb.BIND_OUT,type: oracledb.NUMBER}.
ret : {dir: oracledb.BIND_OUT,type: oracledb.STRING}.
});
console.log('max ACTIVE ver: ' + result.outBinds.active_ver);
console.log('max ARCHIVED ver: ' + result.outBinds.archived_ver);
console.log('max DRAFT ver: ' + result.outBinds.draft_ver);
console.log('Output: ' + result.outBinds.ret);
return result.outBinds.ret;
}
在输出中我得到:
max ACTIVE ver: 0
max ARCHIVED ver: 22
max DRAFT ver: 11.01
Output: ACTIVE
我的问题是为什么我在 sql developer 中得到了一个正确的值,它是 ARCHIVED 但错误的输出是由 oracle db 生成的 ACTIVE???
你能帮忙吗?
在您的 checkStatus() 函数中,参数称为 x、xx 和 xxx,但在函数内部您使用 archived_ver、active_ver 和 draft_ver。 我建议您参考函数内的参数名称,现在是 x、xx 和 xxx(但是您可能想提供更有意义的名称。:) )
从这样的事情开始:
'use strict';
const oracledb = require('oracledb');
let config = require('./dbconfig.js');
async function executeFunction() {
let connection;
const fn =
`CREATE OR REPLACE FUNCTION checkStatus(p_archived_ver in number, p_active_ver in number, p_draft_ver in number) return varchar2 is
z varchar2(20);
BEGIN
IF (p_archived_ver > p_active_ver) then
IF (p_draft_ver > p_archived_ver) then
z := 'DRAFT';
ELSE
z := 'ARCHIVED';
END IF;
ELSE
z := 'ACTIVE';
END IF;
RETURN z;
END;`;
const query =
`DECLARE
active_ver number;
archived_ver number;
draft_ver number;
ret varchar2(10);
BEGIN
SELECT 0 into active_ver from dual; -- returns 0
SELECT 16 into archived_ver from dual; -- returns 16
SELECT 11.6 into draft_ver from dual; -- returns 11.6
:ret := checkStatus(active_ver, archived_ver, draft_ver);
END;`;
try {
connection = await oracledb.getConnection(config);
await connection.execute(fn);
const result = await connection.execute(
query,
{ ret: {dir: oracledb.BIND_OUT,type: oracledb.STRING} });
console.log('Output: ' + result.outBinds.ret);
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
executeFunction();