在显式游标中使用 'Insert Into' 和 'update' 关键字

Using 'Insert Into' with an 'update' keyword in an Explicit Cursor

运行 下面的脚本产生错误:

*"报错- ORA-06550:第 19 行,第 15 列: PL/SQL: ORA-00942: table 或视图不存在 ORA-06550:第 19 行,第 1 列: PL/SQL: SQL 语句被忽略 06550.00000 - “行 %s,列 %s:\n%s” *原因:通常是 PL/SQL 编译错误。 行动: "

DECLARE
h_d_regionID departments.region%TYPE;
h_l_countryID locations.country_id%TYPE;

CURSOR DCursor IS
SELECT  d.region --,l.country_id
FROM locations l 
inner join departments d
on d.location_id = l.location_id
FOR UPDATE;
BEGIN
OPEN DCursor;
LOOP
FETCH DCursor INTO h_l_countryID;
EXIT WHEN DCursor%NOTFOUND;
/* Delete the current tuple: */
--DELETE FROM departments.region WHERE CURRENT OF DCursor;
/* Insert the reverse tuple: */
INSERT INTO d.region VALUES(h_d_regionID);

END LOOP;
/* Free cursor used by the query. */
CLOSE DCursor;
END;

我在这里错过了什么?

您在 INSERT INTO 语句中使用了带有别名 (D) 的 table 名称。

以下代码应该有效(请参阅内联注释以获取解决方案)

DECLARE
    H_D_REGIONID    DEPARTMENTS.REGION%TYPE;
    H_L_COUNTRYID   LOCATIONS.COUNTRY_ID%TYPE;
    CURSOR DCURSOR IS
    SELECT D.REGION --,l.country_id
      FROM LOCATIONS     L
     INNER JOIN DEPARTMENTS   D
    ON D.LOCATION_ID = L.LOCATION_ID
    FOR UPDATE;

BEGIN
    OPEN DCURSOR;
    LOOP
        FETCH DCURSOR INTO H_L_COUNTRYID;
        EXIT WHEN DCURSOR%NOTFOUND;
/* Delete the current tuple: */
--DELETE FROM departments.region WHERE CURRENT OF DCursor;
/* Insert the reverse tuple: */
        --INSERT INTO D.REGION VALUES ( H_D_REGIONID ); --issue is with this line
        INSERT INTO REGION VALUES ( H_D_REGIONID ); -- removed the D. before table name
    END LOOP;
/* Free cursor used by the query. */

    CLOSE DCURSOR;
END;

此外,请注意,您在 INSERT 语句中使用了 H_D_REGIONID,但它没有被初始化或分配任何值。