将数据从Oracle中的其他表中插入表

时间:2015-09-02 09:08:02

标签: oracle

我有两个相同的Oracle表,它们具有相同的列名。

1)DoctorProfile_New
2)DoctorProfile_Old

两个表共享相同的结构如下。

DOCREGNO      NOT NULL VARCHAR2(10)
DOCCATOGARY          NOT NULL VARCHAR2(10)
ANAME1                      VARCHAR2(50)
ANAME2                      VARCHAR2(50)
ANAME3                      VARCHAR2(50)
ANAME4                      VARCHAR2(50)
ANAME5                      VARCHAR2(50)
ANAME6                      VARCHAR2(50)
ANAME7                      VARCHAR2(50)
ANAME8                      VARCHAR2(50)
ANAME9                      VARCHAR2(50)
AGENDER                     VARCHAR2(2)
ARESCTOWN                   VARCHAR2(100)
DOCRCODE                     NUMBER

我需要做的是将表格 DoctorProfile_Old 中的所有数据插入 DoctorProfile_New 。 唯一的区别是列DOCREGNO,我必须插入新的硬编码值。

insert into DoctorProfile_New
(
DOCREGNO,       
DOCCATOGARY,    
ANAME1,         
ANAME2,         
ANAME3,         
ANAME4,         
ANAME5,         
ANAME6,         
ANAME7,         
ANAME8,         
ANAME9,         
AGENDER,        
ARESCTOWN
)
values
('000081',
(
select 
DOCCATOGARY,    
ANAME1,         
ANAME2,
ANAME3,         
ANAME4,         
ANAME5,         
ANAME6,
ANAME7,         
ANAME8,         
ANAME9,         
AGENDER,        
ARESCTOWN 
from DoctorProfile_Old  WHERE DOCREGNO='T07004'
)
)

我尝试了上面的查询,它总是给出Oracle错误 ORA-00947: not enough values

2 个答案:

答案 0 :(得分:2)

这个怎么样?

insert into DoctorProfile_New
(DOCREGNO,DOCCATOGARY,ANAME1,ANAME2,ANAME3,ANAME4,ANAME5,ANAME6,ANAME7,ANAME8,ANAME9,AGENDER,ARESCTOWN)
select '000081' as DOCREGNO, DOCCATOGARY,ANAME1,ANAME2,ANAME3,ANAME4,ANAME5,ANAME6,ANAME7,ANAME8,ANAME9,AGENDER,ARESCTOWN from DoctorProfile_Old  WHERE DOCREGNO='T07004';

如果要自动插入DOCREGNO而不是硬涂层值,则应创建触发器并使用序列值。

CREATE OR REPLACE TRIGGER DoctorProfile_New_TRG
BEFORE INSERT OR UPDATE ON DoctorProfile_New
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
BEGIN

    if :new.DOCREGNO is null then
        select lpad(some_sequence.nextval, 6, '0') into :new.DOCREGNO from dual;
    end if;

END;

插入DoctorProfile_Old的所有数据

insert into DoctorProfile_New
(DOCREGNO,DOCCATOGARY,ANAME1,ANAME2,ANAME3,ANAME4,ANAME5,ANAME6,ANAME7,ANAME8,ANAME9,AGENDER,ARESCTOWN)
select null,DOCCATOGARY,ANAME1,ANAME2,ANAME3,ANAME4,ANAME5,ANAME6,ANAME7,ANAME8,ANAME9,AGENDER,ARESCTOWN from DoctorProfile_Old;

答案 1 :(得分:2)

您可以使用insert select执行此操作;

insert into DoctorProfile_New (
    DOCREGNO,       
    DOCCATOGARY,    
    ANAME1,         
    ANAME2,         
    ANAME3,         
    ANAME4,         
    ANAME5,         
    ANAME6,         
    ANAME7,         
    ANAME8,         
    ANAME9,         
    AGENDER,        
    ARESCTOWN
)
select 
    '000081',
    DOCCATOGARY,    
    ANAME1,         
    ANAME2,
    ANAME3,         
    ANAME4,         
    ANAME5,         
    ANAME6,
    ANAME7,         
    ANAME8,         
    ANAME9,         
    AGENDER,        
    ARESCTOWN 
from DoctorProfile_Old  
WHERE DOCREGNO='T07004';