 CREATE TRIGGER `trig_update_item` AFTER UPDATE ON `items`
  FOR EACH ROW BEGIN
 
+-- Volumen eines Exemplars abrufen
+DECLARE var_vol_cbm decimal(10,4) DEFAULT 0;
+SET var_vol_cbm = (SELECT `AJ_Volumen in cbm` FROM data_export WHERE NEW.item_import_id = `B__Index` AND (SELECT department_name FROM departments WHERE department_id = OLD.item_department_id) = `D__Dezernat\/Fachbereich` AND OLD.item_description = `AE_Bezeichnung` AND OLD.item_state = `AR_Zustand` AND (SELECT room_name FROM rooms WHERE room_id = OLD.item_room_id) = `J__Raum-Nr. Bestand` OR (SELECT room_name FROM rooms WHERE room_id = OLD.item_room_id) = `Q__Raum-Nr. neu (Raum-ID)`)/(SELECT `AD_Anzahl` FROM data_export WHERE NEW.item_import_id = `B__Index` AND (SELECT department_name FROM departments WHERE department_id = OLD.item_department_id) = `D__Dezernat\/Fachbereich` AND OLD.item_description = `AE_Bezeichnung` AND OLD.item_state = `AR_Zustand` AND (SELECT room_name FROM rooms WHERE room_id = OLD.item_room_id) = `J__Raum-Nr. Bestand` OR (SELECT room_name FROM rooms WHERE room_id = OLD.item_room_id) = `Q__Raum-Nr. neu (Raum-ID)`);
+
 -- Fachbereich, Bezeichnung, Zustand oder Raum eines Items geändert?
 IF NEW.item_department_id != OLD.item_department_id OR NEW.item_description != OLD.item_description OR NEW.item_state != OLD.item_state OR NEW.item_room_id != OLD.item_room_id THEN
 
-	-- Aus ursprünglichem Datensatz entfernen
+  -- Aus ursprünglichem Datensatz entfernen
     UPDATE data_export
-    SET AD_Anzahl = AD_Anzahl - 1
+    SET `AJ_Volumen in cbm` = `AJ_volumen in cbm` - var_vol_cbm, AD_Anzahl = AD_Anzahl - 1
     WHERE NEW.item_import_id = `B__Index`
-    	AND (SELECT department_name FROM departments WHERE department_id = OLD.item_department_id) = `D__Dezernat\/Fachbereich` 
+      AND (SELECT department_name FROM departments WHERE department_id = OLD.item_department_id) = `D__Dezernat\/Fachbereich` 
         AND OLD.item_description = `AE_Bezeichnung`
         AND OLD.item_state = `AR_Zustand`
         AND (SELECT room_name FROM rooms WHERE room_id = OLD.item_room_id) = `J__Raum-Nr. Bestand` 


-- Aus ursprünglichem Datensatz entfernen
UPDATE data_export
SET AD_Anzahl = AD_Anzahl - 1
WHERE OLD.item_import_id = `B__Index`
	AND (SELECT department_name FROM departments WHERE department_id = OLD.item_department_id) = `D__Dezernat\/Fachbereich` 
    AND OLD.item_description = `AE_Bezeichnung`
    AND OLD.item_state = `AR_Zustand`;
        
    -- Prüfen, ob der alte Datensatz noch Items enthält (Anzahl > 0)
IF EXISTS(SELECT * FROM data_export WHERE `AD_Anzahl` < 1) THEN
  	DELETE FROM data_export WHERE `AD_Anzahl` < 1;
END IF;

END

        
    -- Prüfen, ob der alte Datensatz noch Items enthält (Anzahl > 0)
    IF EXISTS(SELECT * FROM data_export WHERE `AD_Anzahl` < 1) THEN
      DELETE FROM data_export WHERE `AD_Anzahl` < 1;
    END IF;

  -- Zu neuem Datensatz hinzufügen bzw. bei altem Datensatz Anzahl erhöhen
  IF NOT EXISTS(
        SELECT B__Index 
        FROM data_export 
        WHERE B__Index = NEW.item_import_id 
          AND `D__Dezernat\/Fachbereich` = (SELECT department_name FROM departments WHERE department_id = NEW.item_department_id)
          AND `AE_Bezeichnung` = NEW.item_description
          AND `AR_Zustand` = NEW.item_state
          AND `J__Raum-Nr. Bestand` = (SELECT room_name FROM rooms WHERE room_id = NEW.item_room_id)
    ) THEN
      
        INSERT INTO data_export VALUES (
          (SELECT `A__Zähler` FROM data_import WHERE NEW.item_import_id = B__Index),
        NEW.item_import_id,
          (SELECT `C__Erfassungsdatum` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT department_name FROM departments WHERE department_id = NEW.item_department_id),
          (SELECT `E__Raumnutzungsart` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `F__Land-KZ` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `G__Liegenschafts-Nr. Bestand` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `H__Bauteil-Nr. Bestand` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `I__Etage Bestand` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `J__Raum-Nr. Bestand` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `K__AP-Nr. Bestand` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `L__a1` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `M__Liegenschafts-Nr. neu` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT building_name FROM buildings, maps, rooms WHERE room_map_id = map_id AND map_building_id = building_id AND room_id = NEW.item_room_id),
          (SELECT map_floor FROM maps, rooms WHERE room_map_id = map_id AND room_id = NEW.item_room_id),
          (SELECT room_name_alt FROM rooms WHERE room_id = NEW.item_room_id),
          (SELECT room_name FROM rooms WHERE room_id = NEW.item_room_id),
          (SELECT `R__AP-Nr. neu` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `S__a1` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `T__Anzahl AP im Quell-Raum` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `U__a3` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `V__Name UZK` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `W__Nachname MA / Raumbezeichung` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `X__Vorname MA` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `Y__Titel` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `Z__Tel. MA` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT AA_a4 FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT AB_Kürzel FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT AC_Code FROM data_import WHERE NEW.item_import_id = B__Index),
          1,
          NEW.item_description,
          (SELECT `AF_Cluster Bezeichnung` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT AG_B FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT AH_T FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT AI_H FROM data_import WHERE NEW.item_import_id = B__Index),
          (var_vol_cbm), -- Volumen für ein Exemplar
          (SELECT `AK_Hersteller/` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AL_Modell/ Ausfuehrung/Material/Fuss/Form/hv/Fuss /Türart/Anzahl` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AM_Sitz-Lehne-Bezug/Polsterung/Sockel/u.a.` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AN_Farbe_Sitz-Lehne_Bezug/Material_Platte/Farbe_Deckplatte/Farbe` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AO_Rechnername/Zollgroesse/Druckername/Montageart/Farbe_Korpus/F` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AP_Seitenverh./Tel-Fax-Nr/Farbe_Fuss_/Farbe_Gestell/Farbe_Front/` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AQ_Weitere Eigenschaft/Rollentyp/drehbar/stapelbar/Rahmenausfueh` FROM data_import WHERE NEW.item_import_id = B__Index),
          NEW.item_state,
          (SELECT `AS_Umzug (1/0)` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AT_De(na) Remon(na)tage erfor(na)derlich` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AU_Inventar-Nr.` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AV_Bemerkungen` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AW_Barcode` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AX_Bild Nr 1` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AY_Bild Nr 2` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `AZ_Bild Nr 3` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `BA_Bild Nr 4` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `BB_Bild Nr 5` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `BC_Bild Nr 6` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `BD_Skizze Nr.` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `BE` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `BF` FROM data_import WHERE NEW.item_import_id = B__Index),
          (SELECT `BG` FROM data_import WHERE NEW.item_import_id = B__Index)
      );
        
    ELSE 
      UPDATE data_export
      SET `AJ_Volumen in cbm` = `AJ_volumen in cbm` + var_vol_cbm,`AD_Anzahl` = `AD_Anzahl` + 1
      WHERE `B__Index` = NEW.item_import_id
          AND `D__Dezernat\/Fachbereich` = (SELECT department_name FROM departments WHERE department_id = NEW.item_department_id)
            AND `AE_Bezeichnung` = NEW.item_description
            AND `AR_Zustand` = NEW.item_state
            AND `J__Raum-Nr. Bestand` = (SELECT room_name FROM rooms WHERE room_id = NEW.item_room_id);
