如何将值传递给对象列,从一个 table 到另一个?

How to pass values to object columns from one table to another?

我有我的主要table像这样

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
) 

然后我创建了 Table 嵌套 table 像这样

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 table.

这是我试过的

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 table?

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

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;

意见

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