如何在后续查询中使用数组(Oracle SQL 19c)

问题描述 投票:0回答:1

我无法正确构造此脚本来输出基于数字数组的联接结果,我经历了一些失败的尝试,我有点迷失了!

我从一个实体键开始,我将此键连接到另一个表,该表生成更多键,这些键连接到另一个表,该表为每个先前的键生成一个新键,使用这最后一组键,我必须连接到决赛桌我将收到每个键的结果。

我有一个脚本可以获取第一组键并将它们分配给一个数组,并且我有一个简单的连接,可以通过手动输入每个键并每次按键运行脚本来工作。

我正在尝试编写脚本,以便生成一个数组,并对数组中的每个数字循环查询。

这是生成数组的查询:

`CREATE OR REPLACE TYPE varray IS TABLE OF number;

DECLARE
      releasableentitykey  varray := varray();
BEGIN
      FOR i IN (SELECT * FROM masterbatchrecordreleasesignat  
              INNER JOIN masterbatchrecord ON masterbatchrecordreleasesignat.releasableentitykey = masterbatchrecord.entitykey
              WHERE masterbatchrecord.entitykey='7884867406')
            LOOP
        releasableentitykey.extend;
        releasableentitykey(releasableentitykey.count) := i.Releasesignaturecollectionenti;
            END LOOP;
 END;`

这可以工作并生成我期望的键列表。

第二个查询不起作用:

SELECT * FROM signature  
INNER JOIN releasesignature ON signature.entitykey = releasesignature.signatureentitykey
INNER JOIN masterbatchrecordreleasesignat ON releasesignature.entitykey = masterbatchrecordreleasesignat.releasesignaturecollectionenti
WHERE masterbatchrecordreleasesignat.releasesignaturecollectionenti IN (SELECT column_value FROM TABLE(releasableentitykey);

我该如何编写脚本并构建它?

sql oracle plsql oracle19c
1个回答
0
投票

您不需要集合,可以在第二个查询中使用第一个查询:

SELECT *
FROM   signature s
       INNER JOIN releasesignature rs
       ON s.entitykey = rs.signatureentitykey
       INNER JOIN masterbatchrecordreleasesignat mbrrs
       ON rs.entitykey = mbrrs.releasesignaturecollectionenti
WHERE  mbrrs.releasesignaturecollectionenti IN (
         SELECT releasesignaturecollectionenti
         FROM   masterbatchrecordreleasesignat mbrrs
                INNER JOIN masterbatchrecord mbr
                ON mbrrs.releasableentitykey = mbr.entitykey
         WHERE  mbr.entitykey = '7884867406'
       );

对于样本数据:

CREATE TABLE signature (entitykey) AS
SELECT 1 FROM DUAL;

CREATE TABLE releasesignature (signatureentitykey, entitykey) AS
SELECT 1, 2 FROM DUAL UNION ALL
SELECT 1, 3 FROM DUAL;

CREATE TABLE masterbatchrecordreleasesignat (
  releasesignaturecollectionenti, releasableentitykey
) AS
SELECT 2, '7884867406' FROM DUAL UNION ALL
SELECT 3, '7884867406' FROM DUAL;

CREATE TABLE masterbatchrecord (entitykey) AS
SELECT '7884867406' FROM DUAL;

输出:

实体键 签名实体密钥 实体键 发布签名系列 可释放实体密钥
1 1 2 2 7884867406
1 1 3 3 7884867406

如果您确实想使用集合和 PL/SQL:

CREATE OR REPLACE TYPE number_list IS TABLE OF number;

然后:

DECLARE
  v_releasableentitykey  number_list;
  v_cur SYS_REFCURSOR;
BEGIN
  SELECT releasesignaturecollectionenti
  BULK COLLECT INTO v_releasableentitykey
  FROM   masterbatchrecordreleasesignat mbrrs
         INNER JOIN masterbatchrecord mbr
         ON mbrrs.releasableentitykey = mbr.entitykey
  WHERE  mbr.entitykey = '7884867406';

  FOR r IN (
    SELECT s.entitykey AS s_entitykey,
           rs.entitykey AS rs_entitykey
    FROM   signature s
           INNER JOIN releasesignature rs
           ON s.entitykey = rs.signatureentitykey
           INNER JOIN masterbatchrecordreleasesignat mbrrs
           ON rs.entitykey = mbrrs.releasesignaturecollectionenti
    WHERE  mbrrs.releasesignaturecollectionenti MEMBER OF v_releasableentitykey
  )
  LOOP
    DBMS_OUTPUT.PUT_LINE(r.s_entitykey || ', ' || r.rs_entitykey);
  END LOOP;
END;
/

哪个输出:

1, 2
1, 3

小提琴

© www.soinside.com 2019 - 2024. All rights reserved.