Using If Statements inside MSSQL Stored Procedures
Adding IF statements to a MSSQL Stored Procedure can help make a query more dynamic. For instance, if we had are deleting a participants from a group and wanted to remove a group after the last person was removed, we would do the following:
DELETE FROM Participants
WHERE ParticipantID = @ParticipantID AND GroupID = @GroupID
IF NOT EXISTS (SELECT ParticipantID FROM Participants WHERE GroupID = @GroupID)
DELETE FROM Groups WHERE GroupID = @GroupID
RETURN