SQL update query and 'subquery returned more than one value'

I am using SQL Server 2008, R2. Have a master table (table A) and try to update it with the values ​​from the temp table (Table B).

SQL Server throws an error stating that the subquery returns more than one value, however, I don’t see how this is possible, since the value returned by the subquery is the primary key of table B.

Here's the request:

UPDATE TableA  
   SET TableA.field = (SELECT TableB.field  
                         FROM TableA 
                   INNER JOIN TableB ON TableA.key = TableB.key) 

Any help is greatly appreciated, as usual!

+3
source share
2 answers

. "TableA.key" A FROM , ( A). TableA.field . :

UPDATE TableA  
   SET TableA.field = (SELECT TableB.field  
                       FROM TableB
                       WHERE TableA.key = TableB.key)
+7

, , (TableB.Field), . - TableB.Key. , TableB.Key - . , , 1 . :

UPDATE TableA
   SET TableA.field = (SELECT Top 1 TableB.field
       FROM TableA
       INNER JOIN TableB ON TableA.key = TableB.key)
+1

Source: https://habr.com/ru/post/1773564/


All Articles