--CREATE OR REPLACE PROCEDURE ER_TO_WV
--AS
DECLARE
  PartKey NUMBER(10);
  LastPartKey NUMBER(10);
  CharKey NUMBER(10);
  ValKey NUMBER(10);
  ErLfdNr NUMBER(3);
  ErKey NUMBER(5);
  CURSOR Cur_Ereig IS
    SELECT ERTEIL, ERMERKMAL, ERWERTNR, ERLFDNR, ERTEXTNR FROM EREIG ORDER BY ERTEIL, ERMERKMAL, ERWERTNR, ERLFDNR;
BEGIN
  LastPartKey := 0;
  OPEN Cur_Ereig;
  FETCH Cur_Ereig INTO PartKey, CharKey, ValKey, ErLfdNr, ErKey;
  while Cur_Ereig%FOUND LOOP
    IF (LastPartKey <> PartKey) THEN
      COMMIT;
      LastPartKey := PartKey;
    END IF;
    UPDATE WERTEVAR SET WV0005 = NVL(WV0005, '') || '(' || TO_CHAR(ErKey) || ')' 
         WHERE WVTEIL = PartKey AND WVMERKMAL = CharKey AND WVUNTERS = 0 AND WVWERTNR = ValKey;
    FETCH Cur_Ereig INTO PartKey, CharKey, ValKey, ErLfdNr, ErKey;
  END LOOP;
  COMMIT;
  CLOSE Cur_Ereig;
END;
/

--DELETE FROM EREIG;