[英]Oracle SQL Update one table column with the value of another table
I have a table A
, where there is a column D_DATE
with value in the form YYYYMMDD
(I am not bothered about the date format). 我有一个表
A
,其中有一个D_DATE
列,其值的格式为YYYYMMDD
(我不介意日期格式)。 I also happen to have another table B, where there is a column name V_TILL
. 我也碰巧有另一个表B,其中有一个列名
V_TILL
。 Now, I want to update the V_TILL column value of table B with the value of D_DATE
column in table A
which happens to have duplicates as well. 现在,我想的值更新表B的V_TILL列值
D_DATE
在表列的A
恰好有重复也。 Meaning, the inner query can return multiple records from where I form a query to update the table. 意思是,内部查询可以从我形成查询以更新表的位置返回多个记录。
I currently have this query written but it throws the error: 我目前已编写此查询,但会引发错误:
ORA-01427: single-row subquery returns more than one row
ORA-01427:单行子查询返回多个行
UPDATE TAB_A t1
SET (V_TILL) = (SELECT TO_DATE(t2.D_DATE,'YYYYMMDD')
FROM B t2
WHERE t1.BR_CODE = t2.BR_CODE
AND t1.BK_CODE = t2.BK_CODE||t2.BR_CODE)
WHERE EXISTS (
SELECT 1
FROM TAB_B t2
WHERE t1.BR_CODE = t2.BR_CODE
AND t1.BK_CODE = t2.BK_CODE||t2.BR_CODE)
PS: BK_CODE
IS THE CONCATENATION OF BK_CODE
and BR_CODE
PS:
BK_CODE
是BK_CODE
和BR_CODE
并BR_CODE
Kindly help me as I am stuck in this quagmire! 由于我陷入了泥潭,请帮助我! Any help would be appreciated.
任何帮助,将不胜感激。
If the subquery returns many values which one do you want to use ? 如果子查询返回许多值,您要使用哪个值?
If any you can use rownum <=1; 如果有的话,您可以使用rownum <= 1; If you know that there is only one value use distinct
如果您知道只有一个值,请使用
SET (V_TILL) = (SELECT TO_DATE(t2.D_DATE,'YYYYMMDD')
FROM B t2
WHERE t1.BR_CODE = t2.BR_CODE
AND t1.BK_CODE = t2.BK_CODE||t2.BR_CODE AND ROWNUM <=1)
or 要么
SET (V_TILL) = (SELECT DISTINCT TO_DATE(t2.D_DATE,'YYYYMMDD')
FROM B t2
WHERE t1.BR_CODE = t2.BR_CODE
AND t1.BK_CODE = t2.BK_CODE||t2.BR_CODE)
above are workarounds. 以上是解决方法。 To do it right you have to analyze why you are getting more than one value.
要正确地做到这一点,您必须分析为什么获得不止一个价值。 Maybe more sophisticated logic is needed to select the right value.
也许需要更复杂的逻辑来选择正确的值。
I got it working with this command: 我可以使用以下命令:
MERGE INTO TAB_A A
USING TAB_B B
ON (A.BK_CODE = B.BK_CODE || B.BR_CODE
AND A.BR_CODE = B.BR_CODE AND B.BR_DISP_TYPE <> '0'
AND ((B.BK_CODE, B.BR_SUFFIX) IN (SELECT BK_CODE,
MIN(BR_SUFFIX)
FROM TAB_B
GROUP BY BK_CODE)))
As mentioned earlier by many, I was missing an extra condition and got it working, otherwise the above mentioned techniques work very well. 正如许多人早先提到的那样,我错过了一个额外的条件并使其正常工作,否则上述技术可以很好地工作。
Thanks to all! 谢谢大家!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.