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.
Results:
Branching logic: The IF/THEN keywords provides the ability to decide upon certain branches of logic for execution based on a boolean condition.
Results:
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.
Results:
Planned-execution: The WAITFOR statement provides the ability to plan execution for a specific date/time.
Planned-execution: The WAITFOR DELAY statement provides the same ability as WAITFOR with the additional ability to determine intervals at which the logic should be executed again.