Question

I've got a target schema adresse where plz is a number and a source schema where plz is varchar2.

The following code throws a ORA-01722 - invalid number error, but I can't determine why. There are about 30 plz like '26189' or '38108' and 5 null values.

create or replace
procedure f1_get_adresses is
  cursor c_adresse is 
   select id, strasse, hausnummer, to_number(to_nchar(postleitzahl)) 
   as postleitzahl, ort, land
   from db2_lsg2.f1_adresse;
  rec c_adresse%rowtype;
  counter number(10);
  val_plz number(10);
begin
  for rec in c_adresse
  loop

    -- PLZ
    select count(*) into counter from postleitzahl 
      where plz = rec.postleitzahl and ort = rec.ort;
    if counter = 0 then
      if rec.postleitzahl is null then
        val_plz := 0;
      else
        val_plz := rec.postleitzahl;
      end if;
      insert into postleitzahl (plz, ort) values (rec.ort, val_plz);
    end if;

    -- Land
    select count(*) into counter from land
      where land_name = rec.land;
    if counter = 0 then
      insert into land (land_name) values (rec.land);
    end if;

    --Adresse
    select count(*) into counter from adresse
      where strasse = rec.strasse
        and hausnummer = rec.hausnummer
        and plz = rec.postleitzahl
        and (select land_name from land where land_name = rec.land) = rec.land;
    if counter = 0 then
      insert into adresse (strasse, hausnummer, plz, land_id)
      values (
        rec.strasse,
        rec.hausnummer,
        rec.postleitzahl,
        (select land_id from land where land_name = rec.land)
      );
    end if;
  end loop;
end;
Was it helpful?

Solution

Not much details you gave us, so let's guess.. What is the type of rec.ort ?

insert into postleitzahl (plz, ort) values (rec.ort, val_plz); 

should it not be (different order)

insert into postleitzahl (plz, ort) values (val_plz, rec.ort);  ?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top