In my stored procedure, I want to fetch data where StudentElectivePaper.SubjectPaperID
is equal to passed parameter value or has no value at all (null value).
I have tried OR statement like
StudentElectivePaper.SubjectPaperID = @SubjectPaperID OR StudentElectivePaper.SubjectPaperID IS NULL
but it doesn't yield fruitful result. Please help!
ALTER PROCEDURE [dbo].[StudentElectiveMappings]
@SubjectPaperID INT,
@ExamPaperID INT,
@ExamID INT,
@SubjectElectiveID INT,
@ExamPaperTypeID INT,
@LCID INT = NULL
AS
BEGIN
SELECT DISTINCT
ViewStudentExamApplicant.StudentID,
ViewStudentExamApplicant.ExamRollNo,
CASE
WHEN StudentElectivePaper.ID IS NULL
THEN CAST(0 AS BIT)
ELSE CAST(1 AS BIT)
END AS 'IsMapped'
FROM
StudentElectivePaper
RIGHT OUTER JOIN
ViewStudentExamApplicant ON StudentElectivePaper.ExamPaperID = ViewStudentExamApplicant.ExamPaperID
AND StudentElectivePaper.ExamID = ViewStudentExamApplicant.ExamID
AND ViewStudentExamApplicant.StudentID = StudentElectivePaper.StudentID
WHERE
ViewStudentExamApplicant.ExamID = @ExamID
AND ViewStudentExamApplicant.ExamPaperID = @ExamPaperID
AND StudentElectivePaper.SubjectPaperID = @SubjectPaperID
AND ViewStudentExamApplicant.ExamPaperTypeID = @ExamPaperTypeID
AND StudentElectivePaper.SubjectElectiveID = @SubjectElectiveID
AND (ViewStudentExamApplicant.LCID = @LCID OR @LCID IS NULL)
ORDER BY
IsMapped DESC, ViewStudentExamApplicant.ExamRollNo