简体   繁体   English

Oracle SQL更新一个表列以及另一表的值

[英]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_CODEBK_CODEBR_CODEBR_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.

相关问题 SQL-更新表基于另一表更改一个列的值 - SQL - Update table changing one column value based on another table Oracle sql-将一个表中的sql行值作为列值计数添加到另一表中 - Oracle sql - Adding sql row value from one table as column value count to another table SQL Server:根据同一表中另一列与另一表的列的匹配值,更新一个表中的列的值 - SQL Server: update value of a column in one table based on the matching value of another column in the same table to another table's column 插入临时表并在一个SQL查询(Oracle)中更新另一个表 - Insert into a temporary table and update another table in one SQL query (Oracle) 从oracle中的另一个表更新表中的列值 - Update a column value in a table from another table in oracle 使用另一个表中的重复值更新 Oracle 表中的列 - Update column in Oracle table with value from another table with duplicates Oracle SQL,试图从选择/联接中获取一个值以用于更新一个表中的一列? - Oracle SQL, trying to get one value from a select/join to use to update one column in one table? 如何使用另一个数据库表作为SQL引用更新一个数据库表中的列值 - How to update a column value in one database table using another database table as a reference with SQL SQL查询更新另一个表的引用一个表的列 - SQL query to update column of one table with reference of another table 从一个表列到另一表的SQL Update记录 - SQL Update records from one table column using another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM