请注意,在下面的查询中,括号内的前两个查询,我添加了两个重复的查询,我确信这不是一个好习惯。我需要在需要该值的任何时候重复此查询。
SQL Server 引发了一个关于不要在 SELECT 关键字内写入 DECLARE 的异常。我能做什么或者我缺少什么来重构它?
SELECT A.StudentId,
(
CASE WHEN (SELECT B.OverwrittenScore
FROM dbo.OverwrittenScores AS B
WHERE B.StudentId = A.StudentId
AND B.AssignmentId = @assignmentId
) IS NOT NULL
THEN (
SELECT B.OverwrittenScore
FROM dbo.OverwrittenScores AS B
WHERE B.StudentId = A.StudentId
AND B.AssignmentId = @assignmentId)
ELSE (-- ANOTHER QUERY, BY THE MOMENT: SELECT 0 )
END
) AS FinalScore
FROM dbo.Students AS A