2014-02-11 197 views
0

我試圖通過學校列表進行循環,如果學校標識和關聯表中的context_id之間沒有交叉引用,請將其插入。循環訪問SQL數組

我recieving以下錯誤:

Erreur(141,3): PL/SQL: SQL Statement ignored 
Erreur(141,24): PLS-00321: expression 'IDS' is inappropriate as the left hand side of an assignment statement 
Erreur(141,28): PL/SQL: ORA-00904: : invalid identifier 
Erreur(143,3): PL/SQL: Statement ignored 
Erreur(143,16): PLS-00302: component 'FIRST' must be declared 

(在下面的代碼塊第一行被認爲是136)

PROCEDURE APPLY_IMPLICITE(P_ID IN TBL_CONTEXTES.ID%TYPE, P_UID IN TBL_ECOLE_CONTEXTES.LAST_UID_MODIFICATION%TYPE) 
IS 
    TYPE IDS IS TABLE OF TBL_CONTEXTES.ID%TYPE INDEX BY PLS_INTEGER; 
    ROW_COUNT NUMBER; 
BEGIN 
    -- Get the IDs of all the schools 
    SELECT ECOLE_ID INTO IDS FROM MV_ECOLES; 
    -- Loop through all the schools 
    FOR i IN IDS.FIRST..IDS.LAST LOOP 
    -- Verify if a row exists 
    SELECT COUNT(*) INTO ROW_COUNT FROM TBL_ECOLE_CONTEXTES WHERE ECOLE_ID = IDS(i) AND ID = P_ID; 
    -- If no row exist, insert. 
    IF ROW_COUNT = 0 THEN 
     INSERT INTO TBL_ECOLE_CONTEXTES (ID, ECOLE_ID, LAST_UID_MODIFICATION) 
     VALUES (P_ID, IDS(i), P_UID); 
    END IF; 
    END LOOP; 

END APPLY_IMPLICITE; 

回答

1

你不應該需要一個循環。您只需插入所有非匹配的行:

查詢看起來東西這樣的:

INSERT INTO TBL_ECOLE_CONTEXTES 
    (ID, ECOLE_ID, LAST_UID_MODIFICATION) 
SELECT PID, ECOLE_ID, P_UID 
FROM TBL_ECOLE_CONTEXTES 
WHERE ECOLE_ID NOT IN (SELECT IDS FROM FROM MV_ECOLES WHERE ID = P_ID) 

你的表結構不完全清楚,但應該是一般的想法。