【问题标题】:Select from table and function从表和函数中选择
【发布时间】:2010-04-19 19:36:20
【问题描述】:

我有一个返回表格的函数。返回的表包含(除其他外)一个 store_id。我可以获取特定 transaction_id 和 city_id 的 store_id,如下所示:

select store_id from table(user2.f_get_store(15, 12345)); 
--where 15 and 12345 are city_id and transation_id

我有另一个表,其中包含交易列表(包括 transaction_id 和 city_id)。我想要一个返回的查询

store_id, city_id, transaction_id

对于事务表中的每个条目。我的第一个猜测是:

select user2.f_get_store(city_id, transaction_id), city_id, transaction_id
from table_name;

(简化掉不重要的细节)

但是,这会产生“ORA-00932:数据类型不一致”错误。我需要如何构造这个查询?

(我正在使用 Oracle)

~~~~~编辑~~~~~

当我尝试时

select user2.f_get_store(city_id, transactioN_id) from table_name;

我收到权限不足错误。我认为这是因为 f_get_store 的所有者与我使用的用户不同。

(我编辑了示例代码以显示该函数由不同的用户拥有)

【问题讨论】:

  • 您是否尝试过对返回类型使用强制转换?
  • 同意 AJ。我不知道为什么你的 id 字段会是字符,但这似乎就是它所暗示的。执行“从表中选择 f_get_store(city_id, transaction_id)”时是否会出现相同的错误?
  • 可能 city_id 或 transaction_id 被视为字符串并隐式转换为数字 - 然后轰炸...为了更有用,您应该发布完整的错误消息(提示哪种类型的转换) 或许还有函数的代码。
  • 完全错误:`ORA-00932:不一致的数据类型:预期 - 得到 USER2.F_GET_STORE_TYPE_TABLE 00932。00000 -“不一致的数据类型:预期 %s 得到 %s”*原因:*操作:行错误: 13柱4"
  • @AJ 的第一条评论。你可以说得更详细点吗?转换函数的返回值,或者传入什么?

标签: sql oracle function ora-00932


【解决方案1】:

我想你想要一个标量子查询:

select (select store_id from table(user2.f_get_store(city_id, transaction_id))) store_id, city_id, transaction_id
from table_name;

【讨论】:

  • 是否可以使用 user2.f_get_store(city_id, transation_id) 而不是硬编码 15, 12345?
  • 糟糕——这就是我的意思,只是在我复制您的原始查询时忘记更改值。编辑以显示最终版本。
猜你喜欢
  • 1970-01-01
  • 2015-08-29
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 2013-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多