出现错误 ORA-00904:"ID_NAME_COLL_TYPE":使用 Oracle 对象创建视图时标识符无效
Getting error ORA-00904: "ID_NAME_COLL_TYPE": invalid identifier while creating view with oracle object
我正在看书Practical Oracle SQL
。在本书的第 2 章中,作者正在创建一个具有用户定义类型的视图。这是查询
create or replace type id_name_type as object (
id integer,
name varchar2(20 char)
);
/
create or replace type id_name_coll_type as table of id_name_type;
/
create or replace view customer_order_products_obj
as
select
customer_id,
max(customer_name) as customer_name,
cast(
collect(
id_name_type(product_id, product_name)
order by product_id
) as id_name_coll_type
) as product_coll
from customer_order_products
group by customer_id, id_name_coll_type;
但是当我尝试 运行 视图查询时,我得到了错误 ORA-00904: "ID_NAME_COLL_TYPE": invalid identifier
然后我缩小了查询范围,只 运行 以下查询。只需 运行 select 并从分组依据中删除名称。
select
customer_id,
max(customer_name) as customer_name,
cast(
collect(
id_name_type(product_id, product_name)
order by product_id
) as id_name_coll_type
) as product_coll
from customer_order_products
group by customer_id;
以上查询给出了以下结果
那么 group by customer_id, id_name_coll_type;
有什么问题呢?为什么在这种情况下我得到 "ID_NAME_COLL_TYPE": invalid identifier
。因为这是一本书的例子,所以我认为它应该按原样工作?视图将如何创建?
谢谢
id_name_coll_type
是数据类型的标识符,而不是列的标识符;您必须按列聚合。
As it is a book example that's why I thought it should work as it is?
书籍可以包含错误;您应该查看本书是否有可能包含更正的勘误表,或者您是否拥有本书的最新版本。如果没有,您可以发送电子邮件给 author/publisher 并让他们知道错误。
How view will create?
完全照原样做,不要在 GROUP BY
子句中包含 id_name_coll_type
标识符。您不需要将集合添加到 GROUP BY
子句,因为 COLLECT
是一个聚合函数。
db<>fiddle here
我正在看书Practical Oracle SQL
。在本书的第 2 章中,作者正在创建一个具有用户定义类型的视图。这是查询
create or replace type id_name_type as object (
id integer,
name varchar2(20 char)
);
/
create or replace type id_name_coll_type as table of id_name_type;
/
create or replace view customer_order_products_obj
as
select
customer_id,
max(customer_name) as customer_name,
cast(
collect(
id_name_type(product_id, product_name)
order by product_id
) as id_name_coll_type
) as product_coll
from customer_order_products
group by customer_id, id_name_coll_type;
但是当我尝试 运行 视图查询时,我得到了错误 ORA-00904: "ID_NAME_COLL_TYPE": invalid identifier
然后我缩小了查询范围,只 运行 以下查询。只需 运行 select 并从分组依据中删除名称。
select
customer_id,
max(customer_name) as customer_name,
cast(
collect(
id_name_type(product_id, product_name)
order by product_id
) as id_name_coll_type
) as product_coll
from customer_order_products
group by customer_id;
以上查询给出了以下结果
那么 group by customer_id, id_name_coll_type;
有什么问题呢?为什么在这种情况下我得到 "ID_NAME_COLL_TYPE": invalid identifier
。因为这是一本书的例子,所以我认为它应该按原样工作?视图将如何创建?
谢谢
id_name_coll_type
是数据类型的标识符,而不是列的标识符;您必须按列聚合。
As it is a book example that's why I thought it should work as it is?
书籍可以包含错误;您应该查看本书是否有可能包含更正的勘误表,或者您是否拥有本书的最新版本。如果没有,您可以发送电子邮件给 author/publisher 并让他们知道错误。
How view will create?
完全照原样做,不要在 GROUP BY
子句中包含 id_name_coll_type
标识符。您不需要将集合添加到 GROUP BY
子句,因为 COLLECT
是一个聚合函数。
db<>fiddle here