【发布时间】:2021-06-21 20:41:46
【问题描述】:
我正在尝试使用合并语句进行 upsert 操作。我想做类似下面的事情,我将目标表和源表都加入到学生表中,以获得只存在于学生表中的唯一键。请记住,我不能加入 StudentID。我的代码中可能有重复项。为了唯一性,我需要加入 schoolID。
Merge into GuardianTo gto inner join StudentTo sto on gto.fkStudentId = sto.StudentID
Using (Select * from GuardianFrom gfrom inner join StudentFrom sfrom on gfrom.fkStudentId = sfrom.StudentID) from
ON gto.guardianId = from.guardianId and sto.SchoolID = from.SchoolID
When....
我需要将目标 GuardianTo 表与 studentTo 表连接起来以获取 SchoolID 的唯一键。并将此 ID 与 From 表匹配。我知道这可以在单独的插入和更新语句中完成(不是合并),但是有没有办法使用合并语句来做类似上面的事情?
【问题讨论】: