Please see my other Database Development articles.
Implementing decision-making into logic
The ability to control how logic is executed is central to all logic within the data layer.
Within T-SQL, the following examples illustrates the primary constructs used for this purpose.
Looping: The WHILE keyword provides the ability to execute deterministic actions for a prescribed number of iterations.
Branching logic: The IF/THEN keywords provides the ability to decide upon certain branches of logic for execution based on a boolean condition.
Branching logic: The CASE statement provides similar decision-making as IF/THEN, but when the same boolean condition is tested repeatedly, this method is much cleaner and easier to read.
Planned-execution: The WAITFOR statement provides the ability to plan execution for a specific date/time.