-- 🗑 Kustuta links-id, mis esinevad intersections_unconnected.id1 väljal

SET myvars.schema TO :'schema';

DO $$
DECLARE
  schema_name TEXT := current_setting('myvars.schema');
BEGIN
  EXECUTE format('
    DELETE FROM %I.links
    WHERE id IN (
      SELECT id1 FROM %I.intersections_unconnected
    )', schema_name, schema_name);

  RAISE NOTICE '✅ Kustutatud links.id väärtused, mis vastasid intersections_unconnected.id1';
END
$$;
