将多个查询的结果合并为SQL Server存储过程中的一个结果

时间:2014-03-27 20:24:28

标签: sql stored-procedures

我需要一些存储过程的帮助。它包含一个运行Select查询的循环。如果它循环三次,我得到的是3个表的结果。如何将结果合并为一个表?

程序如下:

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

谢谢,非常感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

在返回表的适当结构的SP的开头创建表变量。在每次迭代中,将选定数据插入该表。从该表中取消分配游标后选择。

CREATE PROCEDURE [dbo].[spGetRndQuestions]  
@ExamCode Nvarchar(60)   AS
BEGIN
    Declare @NosQues Int, @Catgry nvarchar(50)

    DECLARE @tbl TABLE(QuestionID int, QuestionDesc ....)

    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)

        INSERT INTO @tbl
        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

SELECT * FROM @tbl
END

答案 1 :(得分:0)

哈姆雷特确实回答了这个问题。但是,您发布的查询可以进行优化,以消除对游标或表变量的需要。以下代码应该很好地做到这一点:

CREATE PROCEDURE [dbo].[spGetRndQuestions]  
    @ExamCode Nvarchar(60)
AS

SELECT
    --A.[category],
    B.*
FROM tblExamDetail A
CROSS APPLY (
    SELECT TOP (A.[NoOfQues])
        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 = A.[ExamCode]
        AND QM.Category = A.[Category]
    ORDER BY NEWID()
) B
WHERE A.[ExamCode] = @ExamCode