比较SQL Server 2012中的两个表
如果我有两个表比较SQL Server 2012中的两个表,sql,sql-server-2012,Sql,Sql Server 2012,如果我有两个表Grades和StudentsMark,如下所示: 表等级 StudentId Mark examId --------------------------- 1 10 1 2 9 2 3 15 1 4 26 3 表学生分数: StudentId Mark examid -----------------------
Grades
和StudentsMark
,如下所示:
表等级
StudentId Mark examId
---------------------------
1 10 1
2 9 2
3 15 1
4 26 3
表学生分数:
StudentId Mark examid
-----------------------------
1 10 1
2 5 2
3 15 1
4 8 3
我想比较这两个表之间的数据,如果分数不同,我想用StudentMark(mark)
替换grade(mark)
如何做到这一点?使用
将
与更新
连接,并通过在WHERE
子句中选中它,只更新一个不同的
查询
update t1
set t1.[Mark] = t2.[Mark]
from [grade] t1
join [StudentMark] t2
on t1.[StudentId] = t2.[StudentId]
where t1.[Mark] <> t2.[Mark];
更新t1
设置t1.[Mark]=t2.[Mark]
从[等级]t1开始
加入[StudentMark]t2
在t1上。[StudentId]=t2。[StudentId]
式中t1.[Mark]t2.[Mark];
您可以发布您尝试过的内容吗?我使用大容量插入将值从文件插入studentsMark表,现在我想使用大容量插入更新值update@Moh您不需要使用批量更新。您只需在insert语句后更新即可。@plaidDK我知道,但我的培训师让我使用it@Moh你也可以说这是一种散装货,因为它是在每个学生身上连接起来的。