Hi Guys,
I want to write a stored procedure in sql server 2012.
in where clause, if one variable is null then search by another variable or if all variables are null then show default all records
What I have tried:
Declare @ESID int =null, @CenterID int =null, @CertificateID int =null, @ModuleID int =null, @CandidateID int =null
Declare @CandidateSessionStatus table(id int, [Status] nvarchar(50))
Declare @CandidateCertificateModuleStatus table(id int, [Status] nvarchar(50))
Insert into @CandidateSessionStatus([Status],id)
values('Scheduled','1'), ('Inprogress','2'), ('Cancelled','3'), ('Completed','4'), ('Absent','5')
Insert into @CandidateCertificateModuleStatus([Status],id)
values('Pending','1'), ('Scheduled','2'), ('Failed','3'), ('Passed','4'), ('Cancelled','5'), ('Absent','6')
Select
c.ID,
c.FirstNameEN + ' ' + c.LastNameEN as 'CandidateName',
c.Email,
c.ContactNumber,
c.UserName,
ct.NameEN as 'Center',
cr.NameEN as 'Certificate', m.NameEN as 'Module',
es.SlotTime, cs.SessionToken,
css.[Status] as 'CandidateSessionStatus',
ccms.[Status] as 'CandidateCertificateModuleStatus',
ccm.PercentageScore
from CandidateSession cs
inner join ExamSession es on cs.ExamSessionID = es.ID
inner join CandidateCertificate cc on cc.ID = cs.CandidateCertificateID
inner join Certification cr on cr.ID = cc.CertificationID
inner join Module m on m.ID = cs.ModuleID
inner join Candidate c on c.ID = cc.CandidateID
inner join Center ct on ct.ID = c.CenterID
inner join CandidateCertificateModule ccm on ccm.CandidateCertificateID = cc.ID and ccm.ModuleID = cs.ModuleID
inner join @CandidateCertificateModuleStatus ccms on ccms.id = ccm.StatusID
inner join @CandidateSessionStatus css on css.id = cs.StatusID
where COALESCE(es.ID = @ESID, cc.ID = @CertificateID, m.ID = @ModuleID, ct.ID = @CenterID, c.ID = @CandidateID)--Error
can anyone please help me.
Thanks