title: Combine results of multiple queries as one result in a SQL Server stored procedure
body: <p>I need some help with a stored procedure. It contains a loop which runs a <code>Select</code> query. What I get is 3 tables with result if it loops thrice. How can I combine the results as one table?</p>
  
  <p>The procedure is as below: </p>
  
  <pre><code>CREATE PROCEDURE [dbo].[spGetRndQuestions]  
      @ExamCode Nvarchar(60)
  AS
  BEGIN
      Declare @NosQues Int, @Catgry nvarchar(50)
  
      DECLARE CategCursor CURSOR FOR 
        (Select Category  From tblExamDetail Where ExamCode = @ExamCode)
  
      OPEN CategCursor 
  
      FETCH NEXT FROM CategCursor INTO @Catgry
  
      WHILE @@FETCH_STATUS  = 0
      BEGIN
          SET @NosQues = (Select NoOfQues from tblExamDetail Where ExamCode=@ExamCode AND Category=@Catgry)
  
          SELECT TOP(@NosQues) QM.QuestionID, QM.QuestionDesc, QM.QuestionMarks, QM.Answer1, QM.Answer2, QM.Answer3, QM.Answer4 FROM tblQuestionMaster QM 
              INNER JOIN tblExamMaster EM ON QM.Dept = EM.Dept AND QM.Location = EM.Location AND QM.QuesModule = EM.ExamModule 
                  Where EM.ExamCode=@ExamCode AND QM.Category =@Catgry 
                  Order by NEWID()
  
          /*SELECT TOP (@NosQues) QuestionID,QuestionDesc,Answer1,Answer2,Answer3,Answer4,QuestionMarks    from [dbo].[tblQuestionMaster] Where Category=@Catgry AND 
          Order by NEWID() */
  
          FETCH NEXT FROM CategCursor INTO @Catgry
      END
  CLOSE CategCursor
  DEALLOCATE CategCursor
  END
  </code></pre>
  
  <p>Thanks, your help is truly appreciated.</p>