1

在我的以下查询中,如果与它们一起使用的选择语句不返回任何内容,我想分别在 @TMarks、@OMarks、@Percentage 中设置 0、0、0

    create procedure [dbo].[TestRecordSelectMInfo]
@GRNo varchar(4),
@SessionId numeric(1),
@TestTypeId numeric(1),
@TMarks int output,
@OMarks numeric(4) output,
@Percentage numeric(4) output,
@Grade varchar(4) output
as 
begin
SELECT Subjects.Subject, Marks.TotalMarks, Marks.PassingMarks, TestRecord.Marks, Result = case when TestRecord.Marks = 'A' then 'A' else case when cast(TestRecord.Marks as numeric) < Marks.PassingMarks then 'F' else 'P' end end FROM Subjects INNER JOIN Marks ON Subjects.SubjectId = Marks.SubjectId INNER JOIN TestRecord ON Subjects.SubjectId = TestRecord.SubjectId AND Marks.TestTypeId = TestRecord.TestTypeId where TestRecord.SessionId = @SessionId and TestRecord.TestTypeId = @TestTypeId and TestRecord.GRNo = @GRno
set @TMarks = (select sum(Marks.TotalMarks) from Marks inner join TestRecord on Marks.TestTypeId = TestRecord.TestTypeId and Marks.SubjectId = TestRecord.SubjectId where TestRecord.SessionId = @SessionId and TestRecord.TestTypeId = @TestTypeId and TestRecord.GRNo = @GRNo and TestRecord.Marks <> 'A' and cast(TestRecord.Marks as numeric) > Marks.PassingMarks )
set @OMarks = (select sum(cast(TestRecord.Marks as numeric)) from Marks inner join TestRecord on Marks.TestTypeId = TestRecord.TestTypeId and Marks.SubjectId = TestRecord.SubjectId where TestRecord.SessionId = @SessionId and TestRecord.TestTypeId = @TestTypeId and TestRecord.GRNo = @GRNo and TestRecord.Marks <> 'A' and cast(TestRecord.Marks as numeric) > Marks.PassingMarks)
set @Percentage = @OMarks / @TMarks * 100;
set @Grade = case
when @Percentage < 50
then
'NIL'
else
case 
when @Percentage < 60
then
'C'
else
case
when @Percentage < 70
then
'B'
else
case
when @Percentage < 80
then
'A'
else
case
when @Percentage <= 100
then
'A+'
else
'FAIL'
end
end
end
end
end
end
GO
4

1 回答 1

2

编辑:没有group by返回null空集的聚合。您可以使用以下方法解决此问题insull

select  @TMarks = IsNull(sum(Marks.TotalMarks),0)
from    Marks  
inner join TestRecord
  on Marks.TestTypeId = TestRecord.TestTypeId 
  and Marks.SubjectId = TestRecord.SubjectId 
where TestRecord.SessionId = @SessionId 
  and TestRecord.TestTypeId = @TestTypeId 
  and TestRecord.GRNo = @GRNo 
  and TestRecord.Marks <> 'A' 
  and cast(TestRecord.Marks as numeric) > Marks.PassingMarks
于 2012-04-21T11:52:05.590 回答