Closed. This question is opinion-based . It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post .
Closed 4 days ago.
Improve this question
I need this at the moment:
update [dbo].[ProductionCostCenter]
set signerId = 999
where ProductionCostCenter.id = ReasonCode.ProductionCostCenterId
and ReasonCode.Id = 77777
My other way of doing this would be to get the ID from the first table, then using it in a second statement to update my value in question.
I am using SQL Server.
While writing this I thought of
update [dbo].[ProductionCostCenter]
set signerId = 999
where id in (select ProductionCostCenterId
from ReasonCode
where Id = 77777)
Is this the better way?
1条答案
按热度按时间7rtdyuoh1#
The second query, that uses
in
and the subquery, should work. I would still recommendexists
here:exists
usually performs better thanin
, especially with the right index in place:This assumes a 1-N relationship between the two tables. If you have a 1-1 relationship (which somehonw seems less likely), a
join
could feel more natural, as commented by Thom A: