SQL Server 2014 – How to Run a Proc Against All Records in a Temp Table

sql serversql server 2014t-sql

I have 4000 records in a temp table with one column. The column is a customerid that I need to update a record for. As of right now the only thing that comes to mind is running an exec statement for all 4000 records. Is there a more efficient way to do this without the use of a cursor?

Best Answer

Why do it RBAR style? Since all you want is to update a a table based in another one you can just do it by using a join.

update updt
set updt.field = 'value'
from dbo.TargetUpdatableTable updt
join #MyTempTable mtt on mtt.ID = updt.ID

This way you can update your table but only those ids are in the temp table.