sql - 更新语句给出表别名错误
问题描述
我无法弄清楚以下语法有什么问题。
alter table #table1 add C21 datetime
update #table1 a
set a.C21 = b.C21_result_lab from #table2 b where a.person_id = b.person_id and
a.source_id = b.source_id
错误信息:
消息 102,级别 15,状态 1,第 363 行“a”附近的语法不正确。
解决方案
这是正确的语法:
alter table #table1 add C21 datetime;
update a
set a.C21 = (
select b.C21_result_lab from #table2 b
where
a.person_id = b.person_id
and
a.source_id = b.source_id
)
from #table1 a
您必须确保选择查询返回的行数不超过 1。
或者您可以通过加入来做到这一点:
update a
set a.C21 = b.C21_result_lab
from #table1 a inner join #table2 b
on a.person_id = b.person_id and a.source_id = b.source_id
推荐阅读
- mongodb - MongoDB 4 个级别的聚合分组
- python - 聚合和分发时间序列数据
- javascript - 从 URL 显示用户想要的 Instagram 照片
- c# - C# WPF 数据触发器
- ruby - 尝试为开发容器运行后安装时找不到 Gem
- mysql - 如何将mysql数据库与omnet集成
- bash - 使用 nohup 顺序运行多个 python 脚本
- ios - 尝试为 DER 权利重新签署应用程序,但没有看到哈希值
- arangodb - ArangoDB Foxx 服务参考错误:未定义 URL
- python-3.x - 尝试接收来自 8.8.8.8 的响应,但无法在 Windows 上接收