1

I am trying to do:

update existingTable
set code = @declaredTable.code
where id = @declaredTable.id

but when I do this I get the error:

Must declare the scalar variable "@declaredTable"

twice.

How do I update values of an existing tables to values from a declared tables?

Why doesn't this work?

Any help would be much appreciated!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sam B
  • 33
  • 3

1 Answers1

3

You need JOIN :

UPDATE e 
      SET e.code = d.code
FROM existingTable e INNER JOIN 
     @declaredTable d
     on d.id = e.id;
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52