我正在创建一个 Oracle 存储过程来从“配置”表中获取记录。 此存储过程接受“varray”类型的输入参数,因为我们希望一次获取多个“configcategories”的记录。 此外,还有一个类型为“SYS_REFCURSOR”的输出参数。
使用下面的测试脚本,我可以将输入传递给存储过程,但无法打印输出。
您能否帮忙从“SYS_REFCURSOR”中提取值并将其打印在 sqldeveloper 控制台上。
最后,这个存储过程将被用 NodeJS 编写的应用程序调用。
存储过程:
CREATE OR REPLACE TYPE configcategoryarr IS
VARRAY(256) OF VARCHAR2(256);
/
CREATE OR REPLACE PROCEDURE get_configurations (
t_configcategory IN configcategoryarr,
c_configurations OUT SYS_REFCURSOR
) IS
BEGIN
IF t_configcategory.count > 0 THEN
FOR i IN t_configcategory.first..t_configcategory.last LOOP
dbms_output.put_line(t_configcategory(i));
OPEN c_configurations FOR SELECT
configcategory,
configid,
configlabel,
configvalue,
configenabled
FROM
configurations
WHERE
configcategory = t_configcategory(i);
END LOOP;
END IF;
END get_configurations;
/
存储过程测试脚本:
SET SERVEROUTPUT ON;
DECLARE
t_cca configcategoryarr;
l_cursor SYS_REFCURSOR;
l_configcategory configurations.configcategory%TYPE;
l_configid configurations.configid%TYPE;
l_configlabel configurations.configlabel%TYPE;
l_configvalue configurations.configvalue%TYPE;
l_configenabled configurations.configenabled%TYPE;
BEGIN
t_cca := configcategoryarr();
t_cca.extend(10);
t_cca(1) := 'Department';
t_cca(2) := 'OU';
get_configurations(t_configcategory => t_cca, c_configurations => l_cursor);
LOOP
FETCH l_cursor INTO
l_configcategory,
l_configid,
l_configlabel,
l_configvalue,
l_configenabled;
EXIT WHEN l_cursor%notfound;
dbms_output.put_line(l_configcategory
|| '_'
|| l_configid
|| '_'
|| l_configlabel
|| '_'
|| l_configvalue
|| '_'
|| l_configenabled);
END LOOP;
CLOSE l_cursor;
END;
/
输出:
Department
OU
PL/SQL procedure successfully completed.
为什么要对每个值运行查询?你可以像这样更有效地做到这一点:
CREATE OR REPLACE TYPE configcategoryarr IS TABLE OF VARCHAR2(256);
CREATE OR REPLACE PROCEDURE get_configurations (
t_configcategory IN configcategoryarr,
c_configurations OUT SYS_REFCURSOR) IS
BEGIN
OPEN c_configurations FOR
SELECT
configcategory,
configid,
configlabel,
configvalue,
configenabled
FROM configurations
WHERE configcategory MEMBER OF t_configcategory;
END get_configurations;