用于确定学生是否符合要求的课程的存储过程

时间:2018-11-01 19:46:56

标签: sql sql-server stored-procedures

我具有正在使用的课程注册系统的此存储过程。我的意图是,如果查询返回的课程未满足学生的学习条件,则返回-1。

前提条件表只有两列; CourseID是该课程,PreReqCourse_ID是该指定课程的必修课程。如果该学生已修完所有预修课程,那么它应该返回值1。即使我对已修了必修课程的学生运行查询,我也始终获得-1的值。任何帮助将不胜感激!

CREATE PROCEDURE CheckPreReq 
    @StudentID INT, 
    @CourseID INT
AS
    DECLARE @theCount INT

    IF EXISTS (SELECT * 
               FROM PreReq 
               INNER JOIN Student_History ON (PreReq.Course_ID = @CourseID)  
               WHERE Student_History.Course_ID != PreReq.PreReqCourse_ID
                 AND Student_History.Student_ID = @StudentID)
    BEGIN
        SET @theCount =-1
    END
    ELSE
    BEGIN
        SET @theCount = 1
    END

    RETURN @theCount    

2 个答案:

答案 0 :(得分:1)

这样的作品行吗?

DECLARE @PreReqsTotal       tinyint
DECLARE @PreReqsFulfilled   tinyint

-- Count pre-req courses.
SELECT  @PreReqsTotal   = COUNT(*)
FROM    PreReq 
WHERE   [CourseID]      = @CourseId

-- Count how many fulfilled by student.
SELECT  @PreReqsFulfilled = count(*)
FROM    Student_History         hist
JOIN    PreReq                  pre
    on  hist.Course_ID  = pre.PreReqCourse_ID
WHERE   pre.CourseID    = @CourseID
    and hist.Student_ID = @StudentID

RETURN CASE WHEN @PreReqsTotal = @PreReqsFulfilled THEN 1 ELSE -1 END

...或类似的内容:

IF EXISTS
(
    SELECT  blah.*
    FROM
    (
        SELECT   pre.*
                ,[PreFulfilled]     = CASE WHEN hist.Course_ID is null THEN 0 ELSE 1 END
        FROM    PreReq              pre
        LEFT JOIN
                Student_History     hist
            on  pre.PreReqCourse_ID = hist.Course_ID
            and hist.Student_ID = @StudentID
        WHERE   pre.CourseID    = @CourseID
    ) blah
    WHERE   blah.[PreFulfilled] = 0         -- Unfulfilled PreReq.
)
BEGIN
    RETURN -1       -- Has an unfulfilled PreReq.
END
    RETURN 1        -- No unfulfilled PreReqs.

答案 1 :(得分:0)

您应该将PreReq表与PreReq.PreReqCourse_ID和Student_History.CourseID列上的Student_History联接(看一下我的示例)。然后您的SP应该可以工作。

--create tmp example tables
IF OBJECT_ID('tempdb..#PreReq') IS NOT NULL DROP TABLE #PreReq

CREATE TABLE #PreReq(
     CourseID int,
     PreReqCourse_ID int
)

--insert Course 3 which depends on Course 2 and 1 in #PreReq
INSERT INTO #PreReq
values(3,2),(3,1)

IF OBJECT_ID('tempdb..#Student_History') IS NOT NULL DROP TABLE #Student_History

CREATE TABLE #Student_History(
     CourseID int not null,
     StudentID int not null
);

--insert Student 1 who has visited Course 1 and 2
insert into #Student_History
VALUES(1,1),(2,1)

--declare variables
DECLARE @CourseID AS INT = 3
       ,@StudentID AS INT = 1
       --COUNT on how many Courses @CourseID depends
       ,@necessaryCourses AS INT 
       --COUNT on how many Courses the Student has taken
       ,@countTakenCourses AS INT
       ,@theCount AS INT 

SET @necessaryCourses = (SELECT count(*) FROM #PreReq WHERE CourseID = @CourseID);


SET @countTakenCourses = (
    SELECT count(*)
    FROM #PreReq p
    --JOIN with Student_History to check if the student has visited the necessary course
    JOIN #Student_History h on p.PreReqCourse_ID = h.CourseID 
    WHERE p.CourseID = @CourseID AND h.StudentID = @StudentID
)

IF @necessaryCourses = @countTakenCourses  
BEGIN
    set @theCount = 1
END
ELSE 
BEGIN 
    set @theCount =  -1
END

SELECT @theCount AS theCount