从多个表中进行选择时的Oracle INSERT

时间:2017-07-11 14:08:14

标签: database oracle oracle11g

此查询有什么问题?我的插入需要从其他表中获取数据,但是当我使用select时,它会给我错误。

以下是查询:

INSERT INTO PAYMENT (
  OWNER_HI,
  ACCOUNT_ID,
  DATE_PAYMENT,
  ACCOUNT_VALUE_BEFORE,
  CURRENCY,EXCHANGE_RATE,
  SUM,
  SUM_USD,
  DATE_INPUT,
  OPERATOR_ID,
  DOCUMENT,
  INVOICE_ID)
VALUES (
  OWNER,
  ID,
  TODAY,
  SALDO,
  CURRENCY,
  RATE,
  50,
  (50 * RATE),
  TODAY,
  386,
  'teste sis',
  null)
(SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD') "NOW" FROM DUAL) TODAY
(SELECT VALUE FROM ACCOUNT WHERE ACCOUNT_ID = 386) SALDO
(SELECT CURRENCY_IDCURRENCY_ID FROM CURRENCY_EXCHANGE WHERE rownum=1 ORDER BY CURRENCY_ID DESC) CURRENCY
(SELECT EXCHANGE_RATE FROM CURRENCY_EXCHANGE WHERE rownum=1 ORDER BY CURRENCY_ID DESC) RATE;

这是错误的:

  

Erro de SQL:ORA-00933:SQL命令未正确结束
     00933. 00000 - " SQL命令未正确结束"

2 个答案:

答案 0 :(得分:2)

也许你的意思更像是

Could not find suitable configuration file.

祝你好运。

答案 1 :(得分:2)

我们不能混用INSERT ... VALUES和INSERT ... SELECT语法。选择其中一个。当您需要来自其他表的值时,您需要INSERT ... SELECT。

您要查询的表之间没有任何关系,因此请使用CROSS JOIN。只要您从每个行中只选择一行,这就不会产生问题。

SELECT EXCHANGE_RATE FROM CURRENCY_EXCHANGE WHERE rownum=1 ORDER BY CURRENCY_ID DESC没有按照您的想法执行,因为ROWNUM在排序之前未分配。要获得最顶级的货币,请在子查询中使用ROW_NUMBER()之类的分析函数并对其进行过滤。

我必须做一些猜测,因为你不清楚你正在实施的所有业务规则,但你需要这样的事情:

INSERT INTO PAYMENT (
  OWNER_HI,
  ACCOUNT_ID,
  DATE_PAYMENT,
  ACCOUNT_VALUE_BEFORE,
  CURRENCY,EXCHANGE_RATE,
  SUM,
  SUM_USD,
  DATE_INPUT,
  OPERATOR_ID,
  DOCUMENT,
  INVOICE_ID)
select  user, -- where does OWNER come from??
  saldo.account_id,
  trunc(sysdate),
  SALDO.value,
  CURRENCY.CURRENCY_ID ,
  CURRENCY.EXCHANGE_RATE ,
  50,
  (50 * CURRENCY.EXCHANGE_RATE ),
  trunc(sysdate),
  386,
  'teste sis',
  null
from ( select CURRENCY_ID, 
              EXCHANGE_RATE, 
              row_number() over (order by CURRENCY_ID DESC ) as rn
       FROM CURRENCY_EXCHANGE   ) currency
cross join 
    (SELECT * FROM ACCOUNT WHERE ACCOUNT_ID = 386) SALDO
where currency.rn = 1

注意:我忽略了将sysdate转换为字符串(作为“TODAY”),因为将日期存储为字符串是非常糟糕的做法。我希望你只是等待从sysdate剥离时间元素,我们也可以通过截断来实现。