如何将一个表中的多行插入另一个表?

时间:2014-03-20 16:59:14

标签: sql oracle

如何创建一个插入语句,将E_PROVIDER_ID值(PROV_ID,PROV_NAME,IDENTITY_ID)插入E_PROVIDER_MAP,其中IDENTITY_ID_TYPE = 125?

我需要插入以将ID表中的值插入到映射表中,以便:

E_PROVIDER_ID.PROV_ID = E_PROVIDER_MAP.I_PROV
E_PROVIDER_ID.E_PROVIDER_ID = E_PROVIDER_ID.IDENTITY_ID
E_PROVIDER_ID.E_PROV_NAME = E_PROVIDER_MAP.I_PROV_NAME
E_PROVIDER_ID.E_PROV_NAME = E_PROVIDER_MAP.E_PROV_NAME

- 创建ID表

create table E_PROVIDER_ID
(
  PROV_ID          VARCHAR2(50),
  PROV_NAME        VARCHAR2(50),
  ID_LINE#         VARCHAR2(50),
  IDENTITY_TYPE_ID VARCHAR2(50),
  ID_TYPE_NAME     VARCHAR2(50),
  IDENTITY_ID      VARCHAR2(50),
)

- INSERT VALUES

insert into E_PROVIDER_ID (PROV_ID, PROV_NAME, ID_LINE#, IDENTITY_TYPE_ID, ID_TYPE_NAME, IDENTITY_ID,)
values ('1005232', 'SMITH, BRIAN EDWARD', '5', '125', 'PROVIDER CODE ID', '020621');
insert into E_PROVIDER_ID (PROV_ID, PROV_NAME, ID_LINE#, IDENTITY_TYPE_ID, ID_TYPE_NAME, IDENTITY_ID,)
values ('1003556', 'SMITH, JOHN STUART', '4', '125', 'PROVIDER CODE ID', '026786');
insert into E_PROVIDER_ID (PROV_ID, PROV_NAME, ID_LINE#, IDENTITY_TYPE_ID, ID_TYPE_NAME, IDENTITY_ID,)
values ('1003595', 'SMITH, RICHARD F.', '5', '125', 'PROVIDER CODE ID', '029091');
insert into E_PROVIDER_ID (PROV_ID, PROV_NAME, ID_LINE#, IDENTITY_TYPE_ID, ID_TYPE_NAME, IDENTITY_ID,)
values ('1003602', 'SMITH, PAUL MATTHEW', '5', '125', 'PROVIDER CODE ID', '029761');
insert into E_PROVIDER_ID (PROV_ID, PROV_NAME, ID_LINE#, IDENTITY_TYPE_ID, ID_TYPE_NAME, IDENTITY_ID,)
values ('1003769', 'SMITH, MARC DROR', '4', '125', 'PROVIDER CODE ID', '029748');
insert into E_PROVIDER_ID (PROV_ID, PROV_NAME, ID_LINE#, IDENTITY_TYPE_ID, ID_TYPE_NAME, IDENTITY_ID,)
values ('1006776', 'SMITH, ULA V.', '6', '125', 'PROVIDER CODE ID', '034415');
insert into E_PROVIDER_ID (PROV_ID, PROV_NAME, ID_LINE#, IDENTITY_TYPE_ID, ID_TYPE_NAME, IDENTITY_ID,)
values ('1006946', 'SMITH, TIMOTHY P.', '7', '125', 'PROVIDER CODE ID', '034687');
insert into E_PROVIDER_ID (PROV_ID, PROV_NAME, ID_LINE#, IDENTITY_TYPE_ID, ID_TYPE_NAME, IDENTITY_ID,)
values ('1007032', 'SMITH, DAVID C.', '7', '125', 'PROVIDER CODE ID', '101754');
commit;

- 创建地图表

create table E_PROVIDER_MAP
(
  I_PROV       NUMBER,
  I_PROV_NAME  VARCHAR2(50),
  E_PROV       NUMBER,
  E_PROV_NAME  VARCHAR2(50),
  I_REC_INST   VARCHAR2(8)
)

2 个答案:

答案 0 :(得分:1)

我认为你只想要insert . . . select语法:

insert into E_PROVIDER_MAP(<columns go here>)
    select PROV_ID, PROV_NAME, IDENTITY_ID
    from E_PROVIDER_ID
    where IDENTITY_ID_TYPE = 125;

我不能轻易告诉&#34; map&#34;列的列表应该是什么?表

编辑:

根据您的评论和对问题的修改:

insert into E_PROVIDER_MAP(I_PROV, IDENTITY_ID, I_PROV_NAME, E_PROV_NAME)
    select PROV_ID, E_PROVIDER_ID, E_PROV_NAME, E_PROV_NAME
    from E_PROVIDER_ID
    where IDENTITY_ID_TYPE = 125;

我注意到这些列似乎与问题中的create table语句无关。

答案 1 :(得分:0)

因为您要从具有VARCHAR列的表中进行选择,所以需要使用强制转换来执行选择插入。

插入E_PROVIDER_MAP(I_PROV,IDENTITY_ID,I_PROV_NAME,E_PROV_NAME)     选择CAST(PROV_ID AS NUMBER),CAST(E_PROV AS NUMBER),I_PROV_NAME,E_PROV_NAME     来自E_PROVIDER_ID     其中IDENTITY_ID_TYPE = 125;