1

所以我有这个简单的 oracle 查询,它返回我预期的记录:

SELECT to_number(substr(nr_key,5,4)) as numero
FROM fattura_pa 
 LEFT JOIN fattura_pa_status on status_id = id
WHERE ute = 'BX' and length(nr_key) < 21
and to_number(substr(nr_key,5,4)) = 88

如果我把它变成这样:

SELECT * FROM ( 
  SELECT to_number(substr(nr_key,5,4)) as numero
  FROM fattura_pa 
    LEFT JOIN fattura_pa_status on status_id = id
  WHERE ute = 'BX' and length(nr_key) < 21
) 
where numero = 88

它给了我“ORA-01722 无效号码”错误

我无法理解为什么,因为我的“numero = 88”实际上是一个数字......

最奇怪的是,如果我对连接进行注释,查询会再次运行:

SELECT * FROM ( 
  SELECT to_number(substr(nr_key,5,4)) as numero
  FROM fattura_pa 
  --LEFT JOIN fattura_pa_status on status_id = id
  WHERE ute = 'BX' and length(nr_key) < 21
) 
where numero = 88

并且连接与“numero”字段无关......

如被问及,我在这里张贴表格:

CREATE TABLE "FE_ENGINE"."FATTURA_PA" 
   (    "UTE" VARCHAR2(2 BYTE) NOT NULL ENABLE, 
    "NR_KEY" VARCHAR2(21 BYTE) NOT NULL ENABLE, 
    "PROGRESSIVO_INVIO" VARCHAR2(20 BYTE) NOT NULL ENABLE, 
    "XML_CREATO" NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE, 
    "XML_FILE_NAME" VARCHAR2(40 BYTE) NOT NULL ENABLE, 
    "POSIZIONE" NUMBER(4,0) DEFAULT 1 NOT NULL ENABLE, 
    "D3_FILE_NAME" VARCHAR2(40 BYTE) NOT NULL ENABLE, 
    "SPEDITA_IL" DATE, 
    "ACCETTATA_IL" DATE, 
    "ARCHIVIATA_IL" DATE, 
    "STATUS_ID" NUMBER(4,0) NOT NULL ENABLE, 
    "RIELABORARE" NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE, 
    "ROW_INSTIME" DATE DEFAULT SYSDATE NOT NULL ENABLE, 
    "ROW_UPTIME" DATE, 
    "NR_KEY_OLD" VARCHAR2(20 BYTE), 
     CONSTRAINT "UQ_FATTURA_PA_FILE_NAME_POSIT" UNIQUE ("XML_FILE_NAME", "POSIZIONE")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "D3_USR"  ENABLE, 
     CONSTRAINT "FATTURA_PA_PK" PRIMARY KEY ("UTE", "NR_KEY", "PROGRESSIVO_INVIO")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "D3_USR"  ENABLE, 
     CONSTRAINT "FK_FATTURA_PA_FATTURA_PA_STATU" FOREIGN KEY ("STATUS_ID")
      REFERENCES "FE_ENGINE"."FATTURA_PA_STATUS" ("ID") ENABLE
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "D3_USR" ;

CREATE TABLE "FE_ENGINE"."FATTURA_PA_STATUS" 
   (    "ID" NUMBER(4,0) NOT NULL ENABLE, 
    "COD_STATUS" VARCHAR2(16 BYTE) NOT NULL ENABLE, 
    "DESC_STATUS" VARCHAR2(100 BYTE) NOT NULL ENABLE, 
     CONSTRAINT "PK_FATTURA_PA_STATUS" PRIMARY KEY ("ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "D3_USR"  ENABLE
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "D3_USR" ;

有什么想法吗?

4

1 回答 1

1

感谢我得到的建议,经过一些测试,我可以让它工作的唯一方法是:

SELECT to_number(numero) FROM ( 
SELECT substr(nr_key,5,4) as numero
FROM fattura_pa 
 LEFT JOIN fattura_pa_status on status_id = id
WHERE ute = 'BX' and length(nr_key) < 21 
) 
where numero = lpad('88',4,'0')
于 2016-12-19T14:56:04.073 回答