This stored procedure is supposed to :
-
Take the
useridandDateas input parameters -
Check whether this
Idbelongs to a teacher or a student -
If the user is a student, it should output his day’s subjects and what each of them will discuss.
-
If the user is a teacher, it should output his day’s classes and what he’ll teach at each of them.
-
The tables which I use are:
WeeklyPlan (Id, Date, Lesson, Plan),Subject (Name, Id),Student (userid, name, class),Teacher (name, userid),TimeTable (subject, day, class).
Now I Wrote this stored procedure (which it is supposed to do what’s written above), but It doesn’t.
So would you please check it & give me ideas if I wrote it wrong? And if I should ask a question in another way, please tell me.
ALTER PROCEDURE dbo.GetDaySubjects
(
@UserId int,
@DateToday DateTime
)
AS
IF EXISTS(SELECT Std_UserID
FROM Student
WHERE (Std_UserID = @UserId))
BEGIN
SELECT WeeklyPlan.Wkp_Body, WeeklyPlan.Wkp_Date, WeeklyPlan.Wkp_lesson
FROM Class INNER JOIN
Student ON Class.Cls_ID = Student.Std_Class INNER JOIN
TimeTable ON Class.Cls_ID = TimeTable.Ttb_Class INNER JOIN
Subject ON TimeTable.Ttb_Subject = Subject.sbj_ID INNER JOIN
WeeklyPlan ON Subject.sbj_Name = WeeklyPlan.Wkp_lesson
WHERE (WeeklyPlan.Wkp_Date = @DateToday)
END
ELSE IF EXISTS(SELECT Tch_UserID
FROM Teacher
WHERE (Tch_UserID = @UserId))
BEGIN
SELECT TimeTable.Ttb_Class, WeeklyPlan.Wkp_lesson, WeeklyPlan.Wkp_Body, Teacher.Tch_ID
FROM Subject INNER JOIN
TimeTable ON Subject.sbj_ID = TimeTable.Ttb_Subject INNER JOIN
WeeklyPlan ON Subject.sbj_Name = WeeklyPlan.Wkp_lesson INNER JOIN
Teacher ON TimeTable.Ttb_Teacher = Teacher.Tch_ID
WHERE (WeeklyPlan.Wkp_Date = @DateToday)
END
Thank you so much.
What I can understand of your database structure and what you want. Is that you are missing in you where statement to sepcify which user that has this schedule.
And the second one:
But I agree on the comment of your question. So I think you might consider splitting this PROC in two. One that fetches the
Studentand one that fetches theTeacher. Then both of theEXISTS(..)is obsolete.