【发布时间】:2023-04-01 06:43:01
【问题描述】:
以如下查询为例:
select p.product_id, p.product_name,
product_type as product_type,
from products
group by p.product_id, p.product_name
union
select p.product_id, p.product_name,
cast(collect(coalesce(product_type, decode(product_description,null,'DESCR' || '-' product_description) as my_type) as product_type,
from products
group by p.product_id, p.product_name
第一个查询中的 select 语句以 varchar 形式返回 product_type,第二个查询中 product_type 的类型为 my_type。 这是导致 ORA-01790: 表达式必须具有与相应表达式相同的数据类型,因为数据类型不相同。
是否可以将第一个查询的 product_type 转换为 my_type 类型?
我尝试如下所示更改第一个查询,但没有成功。
select p.product_id, p.product_name,
cast(product_type as my_type) as product_type,
decode(product_source_location, null, 'NO_SOURCE', product_source_location)
from products
group by p.product_id, p.product_name
编辑
my_type 定义为'TYPE "my_type" AS TABLE OF varchar2(4000)'
【问题讨论】:
-
有可能吗?当你尝试时发生了什么?如果没有,您可以在 my_type 字段上运行 to_char 吗?
-
当我尝试它时,它给了我一个 [1]: ORA-00902: invalid datatype error。
-
my_type 被定义为 'TYPE "my_type" AS TABLE OF varchar2(4000)'
-
我怀疑第一个 SELECT 是否正确,因为
product_type和product_source_location都不是 GROUP BY 的一部分。 -
我已更正查询。
标签: sql oracle collections oracle10g oracle11g