PL/SQL - 使用由其他游标填充的表声明游标时出错

PL/SQL - Error when declaring a cursor with tables filled by other cursors

我在尝试创建一个游标时遇到问题,该游标在调用其他两个游标后使用 table 填充,Oracle 引发了 ORA-00942 异常 - table 或视图不存在。我尝试初始化 tables 但它没有改变任何东西...

想法是恢复买入价(tsc.prxtar 当 tsc.achvte='A)和卖出价(tsc.prxtar 当 tsc.achvte='V')来自 table,将结果存储在两个不同的 table 中并在最后加入它们。

有人有什么建议吗?也许更简单、更有效的方法?

提前致谢!

DECLARE

CURSOR cursorA IS
    SELECT pro.codpro as CodeProduit,
            pro.nompro as NomProduit,
            tsc.prxtar as PrixAchat
    FROM pro
    INNER JOIN tsc ON pro.codpro=tsc.codpro
    WHERE tsc.achvte='A';
TYPE tableA IS TABLE OF cursorA%ROWTYPE;
tabA tableA:=tableA();

CURSOR cursorV IS
    SELECT pro.codpro as CodeProduit,
            pro.nompro as NomProduit,
            tsc.prxtar as PrixVente
    FROM pro
    INNER JOIN tsc ON pro.codpro=tsc.codpro
    WHERE tsc.achvte='V';
TYPE tableV IS TABLE OF cursorV%ROWTYPE;
tabV tableV:=tableV();

CURSOR cursorAV IS
    SELECT tabA.CodeProduit,
            tabA.NomProduit,
            tabA.PrixAchat,
            tabV.PrixVente
    FROM tabA
    INNER JOIN tabV ON tabA.CodeProduit=tabV.CodeProduit;
                   -- AND tabA.NomProduit=tabB.NomProduit;
TYPE tableAV IS TABLE OF cursorAV%ROWTYPE;
tableauDesPrix tableAV:=tableAV();

BEGIN
OPEN cursorA;
FETCH cursorA BULK COLLECT INTO tabA;
CLOSE cursorA;

OPEN cursorV;
FETCH cursorV BULK COLLECT INTO tabV;
CLOSE cursorV;

OPEN cursorAV;
FETCH cursorAV BULK COLLECT INTO tableauDesPrix;
CLOSE cursorAV;

END;

您不能将游标名称用作上一个游标(cursorAV) 中的table名称。

但我认为您可以使用如下单个查询来实现此目的:

SELECT PRO.CODPRO    AS CODEPRODUIT,
       PRO.NOMPRO,
       TSCA.PRXTAR   AS PRIXACHAT,
       TSCV.PRXTAR   AS PRIXVENTE
  FROM PRO
 INNER JOIN TSCA
ON PRO.CODPRO = TSCA.CODPRO
 INNER JOIN TSCV
ON PRO.CODPRO = TSCV.CODPRO
 WHERE TSCA.ACHVTE = 'A'
   AND TSCV.ACHVTE = 'V';

"Does someone has any advice? Maybe an easier, more efficient way?"

为什么不写一个 SELECT 语句将 PRO 连接到 TSC 两次?

SELECT pro.codpro  as CodeProduit,
       pro.nompro  as NomProduit,
       tsca.prxtar as PrixAchat,
       tscv.prxtar as PrixVente
FROM       pro
INNER JOIN tsc tsca ON pro.codpro = tsca.codpro
INNER JOIN tsc tscv ON pro.codpro = tscv.codpro
WHERE tsca.achvte = 'A'
AND   tscv.achvte = 'V';

SQL 针对联接进行了优化。尽可能在 Plain Old SQL 中做所有事情会更有效率。 (在某些极端情况下,我们可能会选择在 PL/SQL 中执行某些操作,即使我们可以在 SQL 中执行此操作,但此处不能。)