1

I have the following update statement

Declare @customerName varchar(40)
Declare @age int

UPDATE [somedatabase].[contact] set age = @age where name = @customerName; 

I also have a seperate table lets call cusHash with names and ages. Names are unique. I would like to read the cusHash and run my update statement for all of its values.

Patryk Uszyński
  • 1,069
  • 1
  • 12
  • 20
nPcomp
  • 8,637
  • 2
  • 54
  • 49

1 Answers1

5

Instead of going over the values of cusHash one by one, it would be much easier to use an update statement with a join clause:

UPDATE co
SET    co.age = ch.age 
FROM   [somedatabase].[contact] co
JOIN   [somedatabase].[cusHash] ch ON co.name = ch.name
Mureinik
  • 297,002
  • 52
  • 306
  • 350