       INNER JOIN "KBTRBConnection" ON "TRBInput".id = "KBTRBConnection".trbinput_id
       INNER JOIN "TRBConfigEntry" ON "KBTRBConnection".id = "TRBConfigEntry".id
       INNER JOIN "Setup" ON "TRBTOMBConnection".setup_id = "Setup".id
-      INNER JOIN "Run" ON "Setup".id = "Run".id
+      INNER JOIN "Run" ON "Setup".id = "Run".setup_id
     AND
       "Run".id = p_run_id
   LOOP

      INNER JOIN "TRBTOMBConnection" ON "TRBOutput".id = "TRBTOMBConnection".trboutput_id
      INNER JOIN "TOMBInput" ON "TRBTOMBConnection".tombinput_id = "TOMBInput".id
      INNER JOIN "Setup" ON "TRBTOMBConnection".setup_id = "Setup".id
      INNER JOIN "Run" ON "Setup".id = "Run".id
    AND
      "Run".id = p_run_id
  LOOP

      INNER JOIN "PMKBConnection" ON "KonradBoardInput".id = "PMKBConnection".konradboardinput_id
      INNER JOIN "PhotoMultiplier" ON "PMKBConnection".photomultiplier_id = "PhotoMultiplier".id
      INNER JOIN "Setup" ON "TRBTOMBConnection".setup_id = "Setup".id
      INNER JOIN "Run" ON "Setup".id = "Run".setup_id
    AND
      "Run".id = p_run_id
  LOOP
