当我将记录插入 table 时,它插入了 0 条记录。虽然没有错误

When I am inserting records into the table it is inserting 0 records. Though there are no errors

CREATE TABLE new_details_staging (
    e_id         NUMBER(10),
    e_name       VARCHAR2(30),
    portal_desc  VARCHAR2(50),
    CONSTRAINT pk_new_details_staging PRIMARY KEY ( e_id )
);

INSERT INTO new_details_staging VALUES (
    11,
    'A',
    'AA'
);

INSERT INTO new_details_staging VALUES (
    22,
    'B',
    'BB'
);

CREATE TABLE lookup_ref (
    ref_id        NUMBER(10),
    ref_typ       VARCHAR2(30),
    ref_typ_desc  NUMBER(20),
    CONSTRAINT pk_lookup_ref PRIMARY KEY ( ref_id )
);

INSERT INTO lookup_ref VALUES (
    181,
    'portal',
    'AA'
);

INSERT INTO lookup_ref VALUES (
    182,
    'portal',
    'BB'
);

CREATE TABLE new_details_main (
    e_id    NUMBER(10),
    e_name  VARCHAR2(30),
    portal  NUMBER(20),
    CONSTRAINT pk_new_details_main PRIMARY KEY ( e_id )
);

当我将记录插入 table 时,它插入了 0 条记录。有人可以帮助解决为什么没有行插入 table 的问题吗?基本上,我想根据暂存 table 列 portal_desc.

从 lookup_ref table 填充查找 ID

插入查询:

insert
    INTO new_details_main (
        e_id,
        e_name,
        portal
    )
SELECT
    n.e_id,
    n.e_name,
    case 
WHEN n.portal_desc = 'AA' THEN (select l.ref_id from lookup_ref where l.ref_typ='portal' and l.ref_typ_desc = 'AA')
when n.portal_desc = 'BB' then (select l.ref_id from lookup_ref where l.ref_typ= 'portal' and l.ref_typ_desc = 'BB') end
FROM new_details_staging n cross join lookup_ref l;

预期输出

您有几个错误的 INSERT

您已将 LOOKUP_REF.REF_TYPE_DESC 定义为 NUMBER(20),但随后您尝试将 'AA' 和 'BB' 的值插入其中。

失败

ORA-01722: invalid number

那么对于你的查询-

SELECT
    n.e_id,
    n.e_name,
    case 
WHEN n.portal_desc = 'AA' THEN (select l.ref_id from lookup_ref where l.ref_typ='portal' and l.ref_typ_desc = 'AA')
when n.portal_desc = 'BB' then (select l.ref_id from lookup_ref where l.ref_typ= 'portal' and l.ref_typ_desc = 'BB') end
FROM new_details_staging n cross join lookup_ref l;

那些带有 AA 和 BB 的行不存在 'joined',所以结果集是空的。

让我们尝试解决这个问题。

ALTER TABLE LOOKUP_REF 
    MODIFY ( REF_TYP_DESC varchar2(20) )
Table ADMIN.LOOKUP_REF altered.

运行 再次插入。

INSERT INTO lookup_ref VALUES (
    181,
    'portal',
    'AA'
);

INSERT INTO lookup_ref VALUES (
    182,
    'portal',
    'BB'
);

1 row inserted.

Elapsed: 00:00:00.009


Copy to clipboard

Show info

1 row inserted.

Elapsed: 00:00:00.005

现在运行再次查询-

更近了。你现在得到

ORA-01427: single-row subquery returns more than one row

你说:

I want to populate the lookup id from the lookup_ref table based on the staging table column portal_desc

这听起来很像应该有一个外键约束。然后你会根据'child'table中的FK值到'look up''parent'table中的详细信息匹配'ID.'那一栏需要唯一或主键约束以确保只有一个匹配值。

除了 LOOKUP_REF.REF_TYP_DESC 的错误声明之外,正如@thatjeffsmith 所指出的,应该将其更改为 VARCHAR2(20),在我看来你不应该对 [= 进行交叉连接11=]。相反,我建议进行内部联接,以便您只联接您关心的行:

INSERT INTO new_details_main
  (e_id, e_name, portal)
SELECT n.e_id,
       n.e_name,
       l.ref_id AS PORTAL
  FROM new_details_staging n
  INNER JOIN lookup_ref l
    ON l.REF_TYP = 'portal' AND
       l.REF_TYP_DESC = n.PORTAL_DESC

这会产生您想要的结果。

db<>fiddle here