I am writing a stored procedure with 3 parameters and my where clause changes depending on one of these parameteres. Is it possible to write a SQL query in this way –
CREATE PROCEDURE [dbo].[VendorVettingModal] @column NVarchar (50), @applicanttype NVarchar (10), @donotuse int AS
declare @column NVarchar (50), @applicanttype NVarchar (10), @donotuse int
select a.Id, a.Firstname, rs.Status,cs.ClearanceStatus
from applicant a
left join ReviewStatus rs on a.ReviewStatus = rs.Id
left join ClearanceStatus cs on a.ClearanceStatus = cs.Id
where
if(@column = 'Recruiting')
begin
a.applicanttype = @applicanttype and a.reviewstatus = 7 and a.donotuse = @donotuse
end
else if(@column = 'Clearance')
begin
a.applicanttype = @applicanttype and (a.reviewstatus != 7 or a.reviewstatus is null) and a.donotuse = @donotuse
end
Rather than writing this way? Because I have about 20-25 columns and a lot more joins and where params than defined here. I have just tried to make it less complicated here.
CREATE PROCEDURE [dbo].[VendorVettingModal] @column NVarchar (50), @applicanttype NVarchar (10), @donotuse int AS
declare @column NVarchar (50), @applicanttype NVarchar (10), @donotuse int
if(@column = 'Recruiting')
begin
select a.Id, a.Firstname, rs.Status,cs.ClearanceStatus
from applicant a
left join ReviewStatus rs on a.ReviewStatus = rs.Id
left join ClearanceStatus cs on a.ClearanceStatus = cs.Id
where
a.applicanttype = @applicanttype and a.reviewstatus = 7 and a.donotuse = @donotuse
end
else if(@column = 'Clearance')
begin
select a.Id, a.Firstname, rs.Status,cs.ClearanceStatus
from applicant a
left join ReviewStatus rs on a.ReviewStatus = rs.Id
left join ClearanceStatus cs on a.ClearanceStatus = cs.Id
where
a.applicanttype = @applicanttype and (a.reviewstatus != 7 or a.reviewstatus is null) and a.donotuse = @donotuse
end
Use parenthesis: