# -*- coding: utf-8 -*-
from ppms.migration import ChangeDTPPacket, PacketCategory, MinVersionDependency, MaxVersionDependency, VersionType


class FixCostCalculationQuery(ChangeDTPPacket):
    """Change query 000368 to check for manual cost calculation"""
    version = 1
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13252

    def get_dependencies(self):
        return (MinVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5', version_type=VersionType.CUSTOMIZING))


    def fix(self, runner):
        queries = {'000002': ("""UPDATE dt461 SET (
DI028310, --Kosten-Soll: Personal intern
DI028317, --Kosten-Soll: Personal extern
DI028320, --Kosten-Soll: Sachkosten
DI028325, --Kosten-Soll: Investitionen
DI028330, --Kosten-Soll: Sonstige
DI028311, --Kosten-Ist: Personal intern
DI028316, --Kosten-Ist: Personal extern
DI028321, --Kosten-Ist: Sachkosten
DI028326, --Kosten-Ist: Investitionen
DI028331, --Kosten-Ist: Sonstige
DI028312, --Kosten-Rest: Personal intern
DI028315, --Kosten-Rest: Personal extern
DI028322, --Kosten-Rest: Sachkosten
DI028327, --Kosten-Rest: Investitionen
DI028332, --Kosten-Rest: Sonstige
DI041540, --Obligo: Personal intern
DI041541, --Obligo: Personal extern
DI041542, --Obligo: Sachkosten
DI041543, --Obligo: Investitionen
DI041544, --Obligo: Sonstige
DI028313, --Kosten-Gesamt: Personal intern
DI028314, --Kosten-Gesamt: Personal extern
DI028323, --Kosten-Gesamt: Sachkosten
DI028328, --Kosten-Gesamt: Investitionen
DI028333, --Kosten-Gesamt: Sonstige
DI028338, --Erloese-Soll
DI028339, --Erloese-Ist
DI028340, --Erloese-Rest
DI028341, --Erloese-Gesamt
-- SPECIAL
DI000661, -- Kosten-Soll
DI000662, -- Kosten-Ist
DI000673, -- Kosten-Rest
DI041545, -- Obligo
DI000674, -- Kosten-Gesamt
DI001039, -- Aufwand-Rest
DI001037, -- Aufwand-Soll
DI001038  -- Aufwand-Ist
)=(SELECT
SUM(DI041635), --Kosten-Soll: Personal intern
SUM(DI041640), --Kosten-Soll: Personal extern
SUM(DI041645), --Kosten-Soll: Sachkosten
SUM(DI041650), --Kosten-Soll: Investitionen
SUM(DI041655), --Kosten-Soll: Sonstige
SUM(DI041636), --Kosten-Ist: Personal intern
SUM(DI041641), --Kosten-Ist: Personal extern
SUM(DI041646), --Kosten-Ist: Sachkosten
SUM(DI041651), --Kosten-Ist: Investitionen
SUM(DI041656), --Kosten-Ist: Sonstige
SUM(DI041637), --Kosten-Rest: Personal intern
SUM(DI041642), --Kosten-Rest: Personal extern
SUM(DI041647), --Kosten-Rest: Sachkosten
SUM(DI041652), --Kosten-Rest: Investitionen
SUM(DI041657), --Kosten-Rest: Sonstige
SUM(DI041638), --Obligo: Personal intern
SUM(DI041643), --Obligo: Personal extern
SUM(DI041648), --Obligo: Sachkosten
SUM(DI041653), --Obligo: Investitionen
SUM(DI041658), --Obligo: Sonstige
SUM(nvl(DI041636,0)+nvl(DI041637,0)), --Kosten-Gesamt: Personal intern --SUM(DI041639)
SUM(nvl(DI041641,0)+nvl(DI041642,0)), --Kosten-Gesamt: Personal extern --SUM(DI041644)
SUM(nvl(DI041646,0)+nvl(DI041647,0)), --Kosten-Gesamt: Sachkosten --SUM(DI041649)
SUM(nvl(DI041651,0)+nvl(DI041652,0)), --Kosten-Gesamt: Investitionen --SUM(DI041654)
SUM(nvl(DI041656,0)+nvl(DI041657,0)), --Kosten-Gesamt: Sonstige --SUM(DI041659)
SUM(DI041660), --Erloese-Soll
SUM(DI041661), --Erloese-Ist
SUM(DI041662), --Erloese-Rest
SUM(nvl(DI041661,0)+nvl(DI041662,0)), --Erloese-Gesamt --SUM(DI041663)
--SPECIAL
SUM(nvl(DI041635,0)+nvl(DI041640,0)+nvl(DI041645,0)+nvl(DI041650,0)+nvl(DI041655,0)), -- Kosten-Soll
SUM(nvl(DI041636,0)+nvl(DI041641,0)+nvl(DI041646,0)+nvl(DI041651,0)+nvl(DI041656,0)), -- Kosten-Ist
SUM(nvl(DI041637,0)+nvl(DI041642,0)+nvl(DI041647,0)+nvl(DI041652,0)+nvl(DI041657,0)), -- Kosten-Rest
SUM(nvl(DI041638,0)+nvl(DI041643,0)+nvl(DI041648,0)+nvl(DI041653,0)+nvl(DI041658,0)), -- Obligo
SUM(nvl(DI041636,0))+SUM(nvl(DI041637,0))+SUM(nvl(DI041641,0))+SUM(nvl(DI041642,0))+
SUM(nvl(DI041646,0))+SUM(nvl(DI041647,0))+SUM(nvl(DI041651,0))+SUM(nvl(DI041652,0))+
SUM(nvl(DI041656,0))+SUM(nvl(DI041657,0)), -- Kosten-Gesamt
SUM(nvl(DI041629,0)+nvl(DI041633,0)),      -- Aufwand-Rest
SUM(nvl(DI041627,0)+nvl(DI041631,0)),      -- Aufwand-Soll
SUM(nvl(DI041628,0)+nvl(DI041632,0))       -- Aufwand-Ist
FROM dt861 WHERE DI023119=DI001001) WHERE di001001 IN (SELECT PROJEKT FROM T_PPMS_RISIKO_VERD)""", """UPDATE dt461 SET (
DI028310, --Kosten-Soll: Personal intern
DI028317, --Kosten-Soll: Personal extern
DI028320, --Kosten-Soll: Sachkosten
DI028325, --Kosten-Soll: Investitionen
DI028330, --Kosten-Soll: Sonstige
DI028311, --Kosten-Ist: Personal intern
DI028316, --Kosten-Ist: Personal extern
DI028321, --Kosten-Ist: Sachkosten
DI028326, --Kosten-Ist: Investitionen
DI028331, --Kosten-Ist: Sonstige
DI028312, --Kosten-Rest: Personal intern
DI028315, --Kosten-Rest: Personal extern
DI028322, --Kosten-Rest: Sachkosten
DI028327, --Kosten-Rest: Investitionen
DI028332, --Kosten-Rest: Sonstige
DI041540, --Obligo: Personal intern
DI041541, --Obligo: Personal extern
DI041542, --Obligo: Sachkosten
DI041543, --Obligo: Investitionen
DI041544, --Obligo: Sonstige
DI028313, --Kosten-Gesamt: Personal intern
DI028314, --Kosten-Gesamt: Personal extern
DI028323, --Kosten-Gesamt: Sachkosten
DI028328, --Kosten-Gesamt: Investitionen
DI028333, --Kosten-Gesamt: Sonstige
DI028338, --Erloese-Soll
DI028339, --Erloese-Ist
DI028340, --Erloese-Rest
DI028341, --Erloese-Gesamt
-- SPECIAL
DI000661, -- Kosten-Soll
DI000662, -- Kosten-Ist
DI000673, -- Kosten-Rest
DI041545, -- Obligo
DI000674, -- Kosten-Gesamt
DI001039, -- Aufwand-Rest
DI001037, -- Aufwand-Soll
DI001038  -- Aufwand-Ist
)=(SELECT
SUM(DI041635), --Kosten-Soll: Personal intern
SUM(DI041640), --Kosten-Soll: Personal extern
SUM(DI041645), --Kosten-Soll: Sachkosten
SUM(DI041650), --Kosten-Soll: Investitionen
SUM(DI041655), --Kosten-Soll: Sonstige
SUM(DI041636), --Kosten-Ist: Personal intern
SUM(DI041641), --Kosten-Ist: Personal extern
SUM(DI041646), --Kosten-Ist: Sachkosten
SUM(DI041651), --Kosten-Ist: Investitionen
SUM(DI041656), --Kosten-Ist: Sonstige
SUM(DI041637), --Kosten-Rest: Personal intern
SUM(DI041642), --Kosten-Rest: Personal extern
SUM(DI041647), --Kosten-Rest: Sachkosten
SUM(DI041652), --Kosten-Rest: Investitionen
SUM(DI041657), --Kosten-Rest: Sonstige
SUM(DI041638), --Obligo: Personal intern
SUM(DI041643), --Obligo: Personal extern
SUM(DI041648), --Obligo: Sachkosten
SUM(DI041653), --Obligo: Investitionen
SUM(DI041658), --Obligo: Sonstige
SUM(nvl(DI041636,0)+nvl(DI041637,0)), --Kosten-Gesamt: Personal intern --SUM(DI041639)
SUM(nvl(DI041641,0)+nvl(DI041642,0)), --Kosten-Gesamt: Personal extern --SUM(DI041644)
SUM(nvl(DI041646,0)+nvl(DI041647,0)), --Kosten-Gesamt: Sachkosten --SUM(DI041649)
SUM(nvl(DI041651,0)+nvl(DI041652,0)), --Kosten-Gesamt: Investitionen --SUM(DI041654)
SUM(nvl(DI041656,0)+nvl(DI041657,0)), --Kosten-Gesamt: Sonstige --SUM(DI041659)
SUM(DI041660), --Erloese-Soll
SUM(DI041661), --Erloese-Ist
SUM(DI041662), --Erloese-Rest
SUM(nvl(DI041661,0)+nvl(DI041662,0)), --Erloese-Gesamt --SUM(DI041663)
--SPECIAL
SUM(CASE WHEN ({0} = 1) THEN nvl(DI041635,0) + nvl(DI041640,0) + nvl(DI041645,0) + nvl(DI041650,0) + nvl(DI041655,0) ELSE DI000661 END), -- Kosten-Soll
SUM(CASE WHEN ({0} = 1) THEN nvl(DI041636,0) + nvl(DI041641,0) + nvl(DI041646,0) + nvl(DI041651,0) + nvl(DI041656,0) ELSE DI000662 END), -- Kosten-Ist
SUM(CASE WHEN ({0} = 1) THEN nvl(DI041637,0) + nvl(DI041642,0) + nvl(DI041647,0) + nvl(DI041652,0) + nvl(DI041657,0) ELSE DI000673 END), -- Kosten-Rest
SUM(CASE WHEN ({0} = 1) THEN nvl(DI041638,0) + nvl(DI041643,0) + nvl(DI041648,0) + nvl(DI041653,0) + nvl(DI041658,0) ELSE DI041545 END), -- Obligo
SUM(CASE WHEN ({0} = 1) THEN nvl(DI041636,0) + nvl(DI041637,0) + nvl(DI041641,0) + nvl(DI041642,0) + nvl(DI041646,0) + nvl(DI041647,0) + nvl(DI041651,0) + nvl(DI041652,0) + nvl(DI041656,0) + nvl(DI041657,0) ELSE DI000674 END), -- Kosten-Gesamt
SUM(CASE WHEN ({0} = 1) THEN nvl(DI041629,0) + nvl(DI041633,0) ELSE DI001039 END),      -- Aufwand-Rest
SUM(CASE WHEN ({0} = 1) THEN nvl(DI041627,0) + nvl(DI041631,0) ELSE DI001037 END),      -- Aufwand-Soll
SUM(CASE WHEN ({0} = 1) THEN nvl(DI041628,0) + nvl(DI041632,0) ELSE DI001038 END)       -- Aufwand-Ist
FROM dt861 WHERE DI023119=DI001001) WHERE di001001 IN (SELECT PROJEKT FROM T_PPMS_RISIKO_VERD)"""),
                   '000004': ("""UPDATE DT461 SET
  DI028310 = DI041635,--Kosten-Soll: Personal intern
  DI028317 = DI041640,--: Personal extern
  DI028320 = DI041645,--: Sachkosten
  DI028325 = DI041650,--: Investitionen
  DI028330 = DI041655,--: Sonstige

  DI028311 = DI041636,--: Personal intern
  DI028316 = DI041641,--: Personal extern
  DI028321 = DI041646,--: Sachkosten
  DI028326 = DI041651,--: Investitionen
  DI028331 = DI041656,--: Sonstige

  DI028312 = DI041637,--: Personal intern
  DI028315 = DI041642,--: Personal extern
  DI028322 = DI041647,--: Sachkosten
  DI028327 = DI041652,--: Investitionen
  DI028332 = DI041657,--: Sonstige

  DI041540 = DI041638,--Obligo: Personal intern
  DI041541 = DI041643,--: Personal extern
  DI041542 = DI041648,--: Sachkosten
  DI041543 = DI041653,--: Investitionen
  DI041544 = DI041658,--: Sonstige

  DI028313 = total_p_int,--Kosten-Gesamt: Personal intern
  DI028314 = total_p_ext,--: Personal extern
  DI028323 = total_material,--: Sachkosten
  DI028328 = total_invest,--: Investitionen
  DI028333 = total_misc,--: Sonstige

  DI028338 = DI041660,--Erloese-Soll
  DI028339 = DI041661,--Erloese-Ist
  DI028340 = DI041662,--Erloese-Rest
  DI028341 = total_revenues,--Erloese-Gesamt

  -- SPECIAL
  DI000661 = total_planed,-- Kosten-Soll
  DI000662 = total_actual,-- Kosten-Ist
  DI000673 = total_remain,-- Kosten-Rest
  DI041545 = total_obligo,-- Obligo
  DI000674 = total_total_costs,-- Kosten-Gesamt
  DI001039 = total_eff_remein,-- Aufwand-Rest
  DI001037 = total_eff_planed,-- Aufwand-Soll
  DI001038  = total_eff_actual  -- Aufwand-Ist

FROM  (Select  DI023119,

SUM(DI041635) DI041635,--Kosten-Soll: Personal intern
SUM(DI041640) DI041640,--: Personal extern
SUM(DI041645) DI041645,--: Sachkosten
SUM(DI041650) DI041650,--: Investitionen
SUM(DI041655) DI041655,--: Sonstige

SUM(DI041636) DI041636,--Kosten-Ist: Personal intern
SUM(DI041641) DI041641,--: Personal extern
SUM(DI041646) DI041646,--: Sachkosten
SUM(DI041651) DI041651,--: Investitionen
SUM(DI041656) DI041656,--: Sonstige

SUM(DI041637) DI041637,--Kosten-Rest: Personal intern
SUM(DI041642) DI041642,--: Personal extern
SUM(DI041647) DI041647,--: Sachkosten
SUM(DI041652) DI041652,--: Investitionen
SUM(DI041657) DI041657,--: Sonstige

SUM(DI041638) DI041638,--Obligo: Personal intern
SUM(DI041643) DI041643,--: Personal extern
SUM(DI041648) DI041648,--: Sachkosten
SUM(DI041653) DI041653,--: Investitionen
SUM(DI041658) DI041658,--: Sonstige

SUM(ISNULL(DI041636,0)+ISNULL(DI041637,0)) total_p_int,--Kosten-Gesamt: Personal intern,
SUM(ISNULL(DI041641,0)+ISNULL(DI041642,0)) total_p_ext,--: Personal extern,
SUM(ISNULL(DI041646,0)+ISNULL(DI041647,0)) total_material,--: Sachkosten,
SUM(ISNULL(DI041651,0)+ISNULL(DI041652,0)) total_invest,--: Investitionen,
SUM(ISNULL(DI041656,0)+ISNULL(DI041657,0)) total_misc,--: Sonstige,

SUM(DI041660) DI041660,--Erloese-Soll
SUM(DI041661) DI041661,--Erloese-Ist
SUM(DI041662) DI041662,--Erloese-Rest
SUM(ISNULL(DI041661,0)+ISNULL(DI041662,0)) total_revenues,--Erloese-Gesamt

--SPECIAL
SUM(ISNULL(DI041635,0)+ISNULL(DI041640,0)+ISNULL(DI041645,0)+ISNULL(DI041650,0)+ISNULL(DI041655,0)) total_planed,-- Kosten-Soll
SUM(ISNULL(DI041636,0)+ISNULL(DI041641,0)+ISNULL(DI041646,0)+ISNULL(DI041651,0)+ISNULL(DI041656,0)) total_actual,-- Kosten-Ist
SUM(ISNULL(DI041637,0)+ISNULL(DI041642,0)+ISNULL(DI041647,0)+ISNULL(DI041652,0)+ISNULL(DI041657,0)) total_remain,-- Kosten-Rest
SUM(ISNULL(DI041638,0)+ISNULL(DI041643,0)+ISNULL(DI041648,0)+ISNULL(DI041653,0)+ISNULL(DI041658,0)) total_obligo,-- Obligo
SUM(ISNULL(DI041636,0))+SUM(ISNULL(DI041637,0))+SUM(ISNULL(DI041641,0))+SUM(ISNULL(DI041642,0))+
SUM(ISNULL(DI041646,0))+SUM(ISNULL(DI041647,0))+SUM(ISNULL(DI041651,0))+SUM(ISNULL(DI041652,0))+
SUM(ISNULL(DI041656,0))+SUM(ISNULL(DI041657,0)) total_total_costs,-- Kosten-Gesamt
SUM(ISNULL(DI041629,0)+ISNULL(DI041633,0)) total_eff_remein,-- Aufwand-Rest
SUM(ISNULL(DI041627,0)+ISNULL(DI041631,0)) total_eff_planed,-- Aufwand-Soll
SUM(ISNULL(DI041628,0)+ISNULL(DI041632,0)) total_eff_actual -- Aufwand-Ist
FROM DT861 GROUP BY DI023119) DT861 WHERE DT861.DI023119=DI001001 AND DI001001 IN (
    SELECT PROJEKT FROM ##T_PPMS_RISIKO_VERD_39440 WHERE SESSIONID = @@SPID);
DELETE FROM ##T_PPMS_RISIKO_VERD_39440 WHERE SESSIONID = @@SPID;""", """UPDATE DT461 SET
  DI028310 = DI041635,--Kosten-Soll: Personal intern
  DI028317 = DI041640,--: Personal extern
  DI028320 = DI041645,--: Sachkosten
  DI028325 = DI041650,--: Investitionen
  DI028330 = DI041655,--: Sonstige

  DI028311 = DI041636,--: Personal intern
  DI028316 = DI041641,--: Personal extern
  DI028321 = DI041646,--: Sachkosten
  DI028326 = DI041651,--: Investitionen
  DI028331 = DI041656,--: Sonstige

  DI028312 = DI041637,--: Personal intern
  DI028315 = DI041642,--: Personal extern
  DI028322 = DI041647,--: Sachkosten
  DI028327 = DI041652,--: Investitionen
  DI028332 = DI041657,--: Sonstige

  DI041540 = DI041638,--Obligo: Personal intern
  DI041541 = DI041643,--: Personal extern
  DI041542 = DI041648,--: Sachkosten
  DI041543 = DI041653,--: Investitionen
  DI041544 = DI041658,--: Sonstige

  DI028313 = total_p_int,--Kosten-Gesamt: Personal intern
  DI028314 = total_p_ext,--: Personal extern
  DI028323 = total_material,--: Sachkosten
  DI028328 = total_invest,--: Investitionen
  DI028333 = total_misc,--: Sonstige

  DI028338 = DI041660,--Erloese-Soll
  DI028339 = DI041661,--Erloese-Ist
  DI028340 = DI041662,--Erloese-Rest
  DI028341 = total_revenues,--Erloese-Gesamt

  -- SPECIAL
  DI000661 = total_planed,-- Kosten-Soll
  DI000662 = total_actual,-- Kosten-Ist
  DI000673 = total_remain,-- Kosten-Rest
  DI041545 = total_obligo,-- Obligo
  DI000674 = total_total_costs,-- Kosten-Gesamt
  DI001039 = total_eff_remein,-- Aufwand-Rest
  DI001037 = total_eff_planed,-- Aufwand-Soll
  DI001038  = total_eff_actual  -- Aufwand-Ist

FROM  (Select  DI023119,

SUM(DI041635) DI041635,--Kosten-Soll: Personal intern
SUM(DI041640) DI041640,--: Personal extern
SUM(DI041645) DI041645,--: Sachkosten
SUM(DI041650) DI041650,--: Investitionen
SUM(DI041655) DI041655,--: Sonstige

SUM(DI041636) DI041636,--Kosten-Ist: Personal intern
SUM(DI041641) DI041641,--: Personal extern
SUM(DI041646) DI041646,--: Sachkosten
SUM(DI041651) DI041651,--: Investitionen
SUM(DI041656) DI041656,--: Sonstige

SUM(DI041637) DI041637,--Kosten-Rest: Personal intern
SUM(DI041642) DI041642,--: Personal extern
SUM(DI041647) DI041647,--: Sachkosten
SUM(DI041652) DI041652,--: Investitionen
SUM(DI041657) DI041657,--: Sonstige

SUM(DI041638) DI041638,--Obligo: Personal intern
SUM(DI041643) DI041643,--: Personal extern
SUM(DI041648) DI041648,--: Sachkosten
SUM(DI041653) DI041653,--: Investitionen
SUM(DI041658) DI041658,--: Sonstige

SUM(ISNULL(DI041636,0)+ISNULL(DI041637,0)) total_p_int,--Kosten-Gesamt: Personal intern,
SUM(ISNULL(DI041641,0)+ISNULL(DI041642,0)) total_p_ext,--: Personal extern,
SUM(ISNULL(DI041646,0)+ISNULL(DI041647,0)) total_material,--: Sachkosten,
SUM(ISNULL(DI041651,0)+ISNULL(DI041652,0)) total_invest,--: Investitionen,
SUM(ISNULL(DI041656,0)+ISNULL(DI041657,0)) total_misc,--: Sonstige,

SUM(DI041660) DI041660,--Erloese-Soll
SUM(DI041661) DI041661,--Erloese-Ist
SUM(DI041662) DI041662,--Erloese-Rest
SUM(ISNULL(DI041661,0)+ISNULL(DI041662,0)) total_revenues,--Erloese-Gesamt

--SPECIAL
SUM(CASE WHEN ({0} = 1) THEN ISNULL(DI041635,0)+ISNULL(DI041640,0)+ISNULL(DI041645,0)+ISNULL(DI041650,0)+ISNULL(DI041655,0) ELSE DI000661 END) total_planed,-- Kosten-Soll
SUM(CASE WHEN ({0} = 1) THEN ISNULL(DI041636,0)+ISNULL(DI041641,0)+ISNULL(DI041646,0)+ISNULL(DI041651,0)+ISNULL(DI041656,0) ELSE DI000662 END) total_actual,-- Kosten-Ist
SUM(CASE WHEN ({0} = 1) THEN ISNULL(DI041637,0)+ISNULL(DI041642,0)+ISNULL(DI041647,0)+ISNULL(DI041652,0)+ISNULL(DI041657,0) ELSE DI000673 END) total_remain,-- Kosten-Rest
SUM(CASE WHEN ({0} = 1) THEN ISNULL(DI041638,0)+ISNULL(DI041643,0)+ISNULL(DI041648,0)+ISNULL(DI041653,0)+ISNULL(DI041658,0) ELSE DI041545 END) total_obligo,-- Obligo
SUM(CASE WHEN ({0} = 1) THEN ISNULL(DI041636,0)+ISNULL(DI041637,0)+ISNULL(DI041641,0)+ISNULL(DI041642,0)+ISNULL(DI041646,0)+ISNULL(DI041647,0)+ISNULL(DI041651,0)+ISNULL(DI041652,0)+ISNULL(DI041656,0)+ISNULL(DI041657,0) ELSE 0 END) total_total_costs,-- Kosten-Gesamt
SUM(CASE WHEN ({0} = 1) THEN ISNULL(DI041629,0)+ISNULL(DI041633,0)ELSE DI000674 END) total_eff_remein,-- Aufwand-Rest
SUM(CASE WHEN ({0} = 1) THEN ISNULL(DI041627,0)+ISNULL(DI041631,0)ELSE DI001037 END) total_eff_planed,-- Aufwand-Soll
SUM(CASE WHEN ({0} = 1) THEN ISNULL(DI041628,0)+ISNULL(DI041632,0)ELSE DI001038 END) total_eff_actual -- Aufwand-Ist
FROM DT861 JOIN DT461 ON DI023119=DI001001 GROUP BY DI023119) DT861 WHERE DT861.DI023119=DI001001 AND DI001001 IN (
    SELECT PROJEKT FROM ##T_PPMS_RISIKO_VERD_39440 WHERE SESSIONID = @@SPID);
DELETE FROM ##T_PPMS_RISIKO_VERD_39440 WHERE SESSIONID = @@SPID;""")}
        errors = 0

        for dbms, (old_query, new_query) in queries.items():
            rec = self.get_record(343, [dbms, '000368'], ['source_text_clob'])
            if not self.change_value(rec, 'source_text_clob', old_query, new_query):
                errors += 1

        if not errors:
            return self.success()
        return self.fail()