Reputation:
I want to do a if-else condition statement in SQL Server but am not sure how.
Inside the stored procedure I have the following parameters:
@MarketId nvarchar (10),
@RegionId nvarchar (10)
And the following statement:
select * from Interaction I
where
(@MarketId = 0 ) OR (I.MarketId = (SELECT Id FROM Market WHERE ExternalId = @MarketId))
What I want to do is to check the value of @MarketId
if @MarketId = 0
then I want the where condition for I.MarketId
to get its Ids from elsewhere like
(SELECT ID FROM Market WHERE ExternalId = @RegionId)
otherwise, if its 1, then I just want to leave it as is and get the Id from @MarketId
instead of @RegionId
..
How should I go about this?
Thanks!
Upvotes: 1
Views: 2533
Reputation: 70678
This should work:
SELECT *
FROM Interaction I
WHERE ( @MarketID = 0
AND EXISTS (SELECT 1 FROM Market
WHERE ExternalId = @RegionId AND Id = I.MarketID)
OR I.MarketID = @MarketID
Upvotes: 4