【问题标题】:pl/sql - ORA-01702 - invalid numberpl/sql - ORA-01702 - 无效号码
【发布时间】:2012-03-09 09:59:12
【问题描述】:

我有一个目标模式 adresse,其中 plz 是一个数字,还有一个源模式,其中 plz 是 varchar2。

以下代码引发 ORA-01722 - 无效数字错误,但我无法确定原因。 大约有 30 个像 '26189' 或 '38108' 和 5 个空值。

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;

【问题讨论】:

  • 到底在哪里抛出错误?
  • 为什么将邮政编码 (PLZ) 存储为数字?这样你就不能存储像'01067'这样的PLZ,因为数字不是用前导零存储的,但是对于正确的PLZ,你必须有前导零。您也应该将目标表中的数据类型更改为varchar2

标签: oracle plsql ora-01722


【解决方案1】:

你给我们的细节不多,所以让我们猜测一下。rec.ort 的类型是什么?

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

不应该是(不同的顺序)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 2023-03-15
    相关资源
    最近更新 更多