本来我想用if / else语句来完成的“UPSERT”,有人建议我用MERGE
我的问题与MERGE
是它看起来并不像我可以用两个来源。
这是我在UPSERT原始尝试:
IF ((SELECT COUNT(CAST(StudentuserID AS int)) FROM HL_StudentAttendance WHERE StudentUserID=1)>0)
UPDATE HL_StudentAttendance
SET
CID = CAST('[querystring:CID]' AS int),
CalendarEventID = CAST('[querystring:CEID]' AS int),
StudentUserID = CAST('[StudentUserID]' AS int),
Attendance = '[Attendance]'
ELSE
INSERT INTO HL_StudentAttendance
(CID,CalendarEventID,StudentUserID,Attendance)
VALUES
(CAST('[querystring:CID]' AS int), CAST('[querystring:CEID]' AS int), CAST('[StudentsUserID]' AS int),'[Attendance]')
尽管IF语句的结果是8
所以8>0
它应该运行我的更新它始终运行插入不知道的if / else逻辑丢失的东西我。
下面是一个尝试MERGE
MERGE
HL_StudentAttendance AS target
USING
HL_CourseRegistrations AS source
ON
target.StudentUserID = source.UserID
AND source.
WHEN MATCHED THEN
UPDATE SET
Attendance = '[Attendance]'
WHEN NOT MATCHED THEN
INSERT (CID, CalendarEventID, StudentUserID, Attendance) VALUES ('[querystring:CID]', '[querystring:CEID]', '[UserID]', '[Attendance]')
;
在这里,我的问题是,我在我的表中的数据HL_CourseEvents
应该用来抢的附加源项是CalendarEventID
否则MERGE
工作,但插入重复的项目进入我的HL_StudentAttendance
因为不存在用于参数CalendarEventID
任何建议表示赞赏。
UPDATE
下面的查询不工作,只不过它插入新记录如果有什么在考勤领域的改变,而不是更新正确。 我怀疑是因为我更新的WHERE子句中是不存在的。 我尝试添加它得到执行错误
MERGE HL_StudentAttendance
USING
(
SELECT cr.CID, ce.CalendarEventID, cr.UserID FROM HL_CourseRegistrations cr
JOIN HL_CalendarEvents ce
ON ce.CID = cr.CID
) tmpTable
ON
HL_StudentAttendance.StudentUserID = tmpTable.UserID
AND HL_StudentAttendance.CalendarEventID = tmpTable.CalendarEventID
WHEN MATCHED THEN
UPDATE
SET
Attendance = 'Attended Late'
WHEN NOT MATCHED THEN
INSERT (CID,CalendarEventID,StudentUserID,Attendance) VALUES ('1','1','1','555')
;
查询结果运行两次。 如果我继续运行它,它总是插入更新的更多,而不是:
ID CID CalendarEventID StudentUserID Attendance
1 1 1 1 Attended Late
2 1 1 1 Attended Late
3 1 1 1 Attended Late
4 1 1 1 Attended Late
5 1 1 1 Attended Late
6 1 1 1 555
7 1 1 1 555
8 1 1 1 555
9 1 1 1 555