如何在输出参数中设置默认值

时间:2012-04-21 11:49:09

标签: sql

在我的后续查询中我想分别在@TMarks,@ OMarks,@ Percentage中设置0,0,0如果与它们一起使用的select语句不返回

    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

1 个答案:

答案 0 :(得分: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