【问题标题】:How to pass values to object columns from one table to another?如何将值从一个表传递到另一个表的对象列?
【发布时间】:2020-07-08 23:23:23
【问题描述】:

我有 我的主表像这样

create table final(
    Province varchar2(50),
    Country varchar2(100),
    Latitude Number(10,0),
    Longitude Number(10,0),
    Cdate varchar2(20),
    Confirmed int,
    killed int,
    Recover int
) 

然后我用这样的嵌套表创建了表

create type virus_Statistic_t as object(
vDate varchar2(20),
infection int,
dead int,
recovered int
)
/

create type virus_Statistic_tlb as table of virus_Statistic_t
/

create type countries_t as object(
    Province_or_State varchar2(50),
    Country_or_Region varchar2(100),
    Lat Number(10,0),
    Longt Number(10,0),
    virus virus_Statistic_tlb
)
/

create table countries of countries_t (
       Lat not null,
       Longt not null
) nested table virus store as virus_ntb;

现在我正在尝试将所有列值从 final 传递到 countries 表。

这是我试过的

INSERT INTO countries(Province_or_State, Country_or_Region, Lat, Longt, vDate, infection, dead, recovered)
SELECT  Province, Country, Latitude, Longitude, Cdate, Confirmed, killed, Recover
FROM final
/

它给出了这个错误

ERROR at line 1:
ORA-00904: "RECOVERED": invalid identifier

如何将所有值从 final 传递到 countries 表?

【问题讨论】:

    标签: oracle oracle11g sqlplus


    【解决方案1】:

    您需要使用类型构造函数。正确的语法是这样的:

    INSERT INTO countries(Province_or_State, Country_or_Region, Lat, Longt, virus)
    SELECT  Province, Country, Latitude, Longitude,
            virus_Statistic_tlb (virus_Statistic_t(Cdate, Confirmed, killed, Recover))
    FROM final
    /
    

    虽然请注意,这只是在每个国家/地区插入一个病毒行,这是您的意思吗?要在每个国家/地区插入多个病毒行,请执行以下操作:

    INSERT INTO countries(Province_or_State, Country_or_Region, Lat, Longt, virus)
    SELECT  Province, Country, Latitude, Longitude,
            CAST(MULTISET(SELECT virus_Statistic_t(Cdate, Confirmed, killed, Recover)
                          FROM   final f2
                          WHERE  f2.Province = f1.Province
                          AND    ...etc.
                          ) AS virus_Statistic_tlb
                 ) 
    FROM final f1
    GROUP BY Province, Country, Latitude, Longitude;
    

    意见

    在回答有关使用嵌套表的问题时,我一定会说使用它们的正确方法是根本不是!在一个真实的数据库中,您应该有一个单独的用于 virus_statistics 的数据库表,并带有国家表的外键。我意识到您可能出于教育目的这样做,但您也应该意识到,在现实生活中没有人应该使用嵌套表。毫无疑问,当您尝试使用这些数据时,您很快就会意识到原因:-)

    【讨论】:

    • 你是救生员@Tony。我浪费了一整天的时间来解决这个问题。你是对的,这只是为了教育目的:)
    • 先生,我能知道如何在一个国家/地区插入多个病毒行吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 2014-01-12
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多