I have the following update where I say that the weight column is updated with the data from the weight column but from the other database and where I tell it to be equal to the batch of the other database and the animal
UPDATE [2_Datos].dbo.tblmvto_lotes
SET peso = (SELECT peso FROM [1_Datos].dbo.tblmvto_lotes)
WHERE codlote = (SELECT codlote FROM [1_Datos].dbo.tblmvto_lotes) AND
animal = (SELECT animal FROM [1_Datos].dbo.tblmvto_lotes)
But I get the following message
Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.
This is because it could be that there are more rows in one than the other, but if I am already making the comparison that the codlote and the animal are the same and the two databases are the same, I only need to update the weight field one by the other, as you can see in the img the data is different is to update it
The problem is that your three subqueries are independent of each other and not related to the
UPDATE
. If you are indeed using SQL Server, you can useFROM
andJOIN
to relate the tables and update with the corresponding values.I would do it by performing a JOIN between the tables of both databases, validating the conditions mentioned.