我在Oracle中使用array参数创建了一个过程:
create or replace PACKAGE my_pkg AS
TYPE USER_ID_TYPE IS TABLE OF VARCHAR2(50) INDEX BY BINARY_INTEGER;
create or replace procedure my_proc (my_id varchar2, ids USER_ID_TYPE)
is
xxxx xxxx xxxx ....
end;
我想在sqldeveloper中测试它。
我该怎么做?
我试过这样:
exec('1010', ('111', '222')) -> fail
exec('1010', ['111', '222']) -> fail
exec('1010' ARRAY('111', '222')) -> fail
我只想在sqldeveloper中使用带有数组的过程。
答案 0 :(得分:6)
在最近发布的18.1版Oracle数据库(可在livesql.oracle.com和Oracle Cloud上免费浏览)中,您可以使用合格的表达式来完成您喜欢的内容:构建一个关联数组"在飞行中"在程序的调用中:
CREATE OR REPLACE PACKAGE my_pkg
AS
TYPE user_id_type IS TABLE OF VARCHAR2 (50)
INDEX BY BINARY_INTEGER;
END;
/
CREATE OR REPLACE PROCEDURE my_proc (
my_id VARCHAR2, ids my_pkg.user_id_type)
IS
BEGIN
DBMS_OUTPUT.put_line ('COUNT = ' || ids.COUNT);
END;
/
BEGIN
my_proc ('steven',
my_pkg.user_id_type (1 => '111', 2=> '222'));
END;
/
Package created.
Procedure created.
COUNT = 2
更详细地查看我的LiveSQL script探索数组的限定表达式。
答案 1 :(得分:3)
编写一个匿名的PL / SQL块来调用它:
Oracle 11g R2架构设置:
CREATE TABLE test (
my_id VARCHAR2(10),
id VARCHAR2(50),
key NUMBER(8,0)
)
/
CREATE PACKAGE my_pkg AS
TYPE USER_ID_TYPE IS TABLE OF VARCHAR2(50) INDEX BY BINARY_INTEGER;
PROCEDURE my_proc(my_id varchar2, ids USER_ID_TYPE);
END;
/
CREATE PACKAGE BODY my_pkg AS
PROCEDURE my_proc(my_id varchar2, ids USER_ID_TYPE)
IS
i BINARY_INTEGER;
BEGIN
i := ids.FIRST;
WHILE i IS NOT NULL LOOP
INSERT INTO test VALUES ( my_id, ids(i), i );
i := ids.NEXT(i);
END LOOP;
END;
END;
/
查询1 :
DECLARE
ids MY_PKG.USER_ID_TYPE;
BEGIN
ids(111) := '222';
ids(42) := 'The Meaning of Life, The Universe and Everything.';
MY_PKG.MY_PROC( '1010', ids );
END;
查询2 :
SELECT * FROM TEST
<强> Results 强>:
| MY_ID | ID | KEY |
|-------|---------------------------------------------------|-----|
| 1010 | The Meaning of Life, The Universe and Everything. | 42 |
| 1010 | 222 | 111 |
...然后你可以创建数组并在构造函数中填充它。
因此,请从类型声明中删除INDEX BY BINARY_INTEGER
:
Oracle 11g R2架构设置:
CREATE TABLE test (
my_id VARCHAR2(10),
id VARCHAR2(50),
key NUMBER(8,0)
)
/
CREATE PACKAGE my_pkg AS
TYPE USER_ID_TYPE IS TABLE OF VARCHAR2(50);
PROCEDURE my_proc(my_id varchar2, ids USER_ID_TYPE);
END;
/
CREATE PACKAGE BODY my_pkg AS
PROCEDURE my_proc(my_id varchar2, ids USER_ID_TYPE)
IS
BEGIN
FOR i IN 1 .. ids.COUNT LOOP
INSERT INTO test VALUES ( my_id, ids(i), i );
END LOOP;
END;
END;
/
查询1 :
BEGIN
MY_PKG.MY_PROC( '1010', MY_PKG.USER_ID_TYPE( '111', '222' ) );
END;
查询2 :
SELECT * FROM TEST
<强> Results 强>:
| MY_ID | ID | KEY |
|-------|-----|-----|
| 1010 | 111 | 1 |
| 1010 | 222 | 2 |