根据 Id [重复] 将行从一个表更新到另一个表
Posted
技术标签:
【中文标题】根据 Id [重复] 将行从一个表更新到另一个表【英文标题】:Update rows from one table to another table based on Id [duplicate] 【发布时间】:2015-02-01 20:28:03 【问题描述】:两个表都有一个 client_id 列。当client_id 列相等时,需要将#LocalDashboardtable 中的数据插入T004_Dashboard。我已经尝试过了,但它无助于显示错误“',' 附近的语法不正确。”
update T004_Dashboard set T004_Dashboard.[GrossCharge],T004_Dashboard.[NetCharge]
= (select #LocalDashboardtable.[GrossCharge] , #LocalDashboardtable.[NetCharge]
from #LocalDashboardtable where
#LocalDashboardtable.client_id =T004_Dashboard.client_id and
#LocalDashboardtable.[month] =T004_Dashboard.[month]
and #LocalDashboardtable.[year] =T004_Dashboard.[year] )
请帮帮我
【问题讨论】:
您使用的是 SQL Server 语法,但问题标记为 mysql。您真正使用的是哪个数据库? 我使用的是 SQL Server2008 R2 【参考方案1】:这是您的查询(看起来很像 SQL Server):
update T004_Dashboard
set T004_Dashboard.[GrossCharge],
T004_Dashboard.[NetCharge] = (select #LocalDashboardtable.[GrossCharge], #LocalDashboardtable.[NetCharge]
from #LocalDashboardtable
where #LocalDashboardtable.client_id = T004_Dashboard.client_id and
#LocalDashboardtable.[month] = T004_Dashboard.[month] and
#LocalDashboardtable.[year] = T004_Dashboard.[year]
);
您不能将一对列设置为子查询中的一对列。相反,使用join
:
update T004_Dashboard
set GrossCharge = ld.GrossCharge,
NetCharge = ld.NetCharge
from T004_Dashboard d join
#LocalDashboardtable ld
on ld.[month] = d.[month] and ld.[year] = d.[year] and
ld.client_id = d.client_id;
此外,SQL Server 不允许在 update/set
语句中使用限定列名
【讨论】:
以上是关于根据 Id [重复] 将行从一个表更新到另一个表的主要内容,如果未能解决你的问题,请参考以下文章