当account_type表中的aif_hyp_acct_type字段为“”、“R”、“L”、“Q”时,我试图更改tdataseg表中的数量字段的符号。aif_hyp_acct_type是主表。它有讨厌的、account和account_type字段。tdataseg表有帐户、金额和许多其他字段。
我尝试了这个查询,得到了或01427错误。
单行子查询返回多行。
update tdataseg
set tdataseg.amount =
(select decode(sign(tdataseg.amount),-1,abs(tdataseg.amount),1,-abs(tdataseg.amount),0)
from tdataseg, aif_hyp_acct_type
where tdataseg.loadid = aif_hyp_acct_type.loadid
and tdataseg.account = aif_hyp_acct_type.account
and aif_hyp_acct_type.account_type in (' ','R','L','Q'))发布于 2014-07-26 00:53:28
问题大概是你认为你有一个相关的子查询,但你没有。外部表在内部查询中被提到。您需要删除该引用:
update tdataseg
set tdataseg.amount = (select decode(sign(tdataseg.amount), -1, abs(tdataseg.amount),
1,-abs(tdataseg.amount), 0)
from aif_hyp_acct_type
where tdataseg.loadid = aif_hyp_acct_type.loadid and
tdataseg.account = aif_hyp_acct_type.account and
aif_hyp_acct_type.account_type in (' ','R','L','Q')
);编辑:
如果没有匹配,并且列被声明为not null,您将得到该错误。这里有一个解决办法:
update tdataseg
set tdataseg.amount = (select coalesce(max(decode(sign(tdataseg.amount), -1, abs(tdataseg.amount),
1,-abs(tdataseg.amount), 0)), 0)
from aif_hyp_acct_type
where tdataseg.loadid = aif_hyp_acct_type.loadid and
tdataseg.account = aif_hyp_acct_type.account and
aif_hyp_acct_type.account_type in (' ','R','L','Q')
);这将将非匹配设置为0。
发布于 2014-07-26 07:11:41
我会尝试用MERGE语句来解决这样的更新问题。我觉得写作更自然一些。
MERGE INTO tdataseg
USING (
SELECT loadid, account
FROM aif_hyp_acct_type
WHERE account_type IN (' ','R','L','Q')
) q
ON (tdataseg.loadid=q.loadid AND tdataseg.account=q.account)
WHEN MATCHED THEN UPDATE SET amount = - ABS(amount);将要更改的表放在MERGE INTO之后。在USING查询中对主表进行子设置。联接条件进入ON子句,并在WHEN MATCHED THEN UPDATE SET之后指定实际的数据更改。
https://stackoverflow.com/questions/24966335
复制相似问题