当我选择它们时,为什么变量会得到空值?

时间:2014-07-22 12:01:10

标签: sql

   ALTER proc showdetails
@quizid int
as 
begin
   declare @result nvarchar(25)
   declare @fail nvarchar(25)
   declare @grade float


   select distinct eq.Quiz_Title , s.NAME , eq.[Start_Date] , eq.End_Date , eqs.Std_Exam_Grad, @result as r , @fail as f  from Exam_Quiz eq

 inner join SUBJECTS s
   on eq.SubjectId = s.SUB_CODE 
   inner join Exam_Quiz_Student eqs 
   on eqs.Quiz_ID = eq.Quiz_ID 
   where eq.Quiz_ID = @quizid


    set @grade = (select distinct eqs.Std_Exam_Grad  from Exam_Quiz_Student eqs where Quiz_ID = @quizid) 

    if(@grade >= 5)
    begin

      set @result = (select distinct COUNT(Std_NID) + ' ' + Std_Exam_Grad + ' ' + Quiz_ID as G  from Exam_Quiz_Student 
                        group by Std_Exam_Grad , Quiz_ID
                        having Std_Exam_Grad > 5 and Quiz_ID = @quizid) 


      --set @result = (select COUNT(Exam_Quiz_StudentID) as success  from Exam_Quiz_Student)


            --set @result = 'pass'
    end
    if(@grade < 5)
    begin

      set @fail = (select distinct COUNT(Std_NID) + ' ' + Std_Exam_Grad + ' ' + Quiz_ID as G from Exam_Quiz_Student 
                      group by Std_Exam_Grad , Quiz_ID
                      having Std_Exam_Grad < 5 and Quiz_ID = @quizid 

                        ) 


                          --set @fail = (select COUNT(Exam_Quiz_StudentID) from Exam_Quiz_Student)





            --set @fail = 'fail'
    end

end

我想知道为什么当我选择它们时变量返回null我想知道在sql查询语句中写入变量的正确方法存储的运行权限但是当我执行它时它返回此错误消息

  

子查询返回的值超过1。这是不允许的   子查询跟随=,!=,&lt;,&lt; =,&gt;,&gt; =或当子查询用作   表达。

1 个答案:

答案 0 :(得分:0)

如果您唯一的问题是变量没有值的原因,您可以尝试使用下面的TOP 1来获取第一条记录中的值。

SELECT TOP 1 @grade =eqs.Std_Exam_Grad  from Exam_Quiz_Student eqs where Quiz_ID = @quizid

但是你会检查你想要实现的功能是否正常。