更新另一个表时必须声明标量变量@tablename [duplicate]

时间:2021-03-28 22:47:36

This question already has an answer here:

这个问题在这里已有答案:

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"

必须声明标量变量“@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!

任何帮助将非常感激!

1 个解决方案

#1


3  

You need JOIN :

你需要加入:

UPDATE e 
      SET e.code = d.code
FROM existingTable e INNER JOIN 
     @declaredTable d
     on d.id = e.id;

#1


3  

You need JOIN :

你需要加入:

UPDATE e 
      SET e.code = d.code
FROM existingTable e INNER JOIN 
     @declaredTable d
     on d.id = e.id;