T-SQL: Control Flow

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.

while_code

Results:

while_results

Branching logic: The IF/THEN keywords provides the ability to decide upon certain branches of logic for execution based on a boolean condition.

ifthen_results

Results:

ifthen_code

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.

case_code

Results:

case_results

Planned-execution: The WAITFOR statement provides the ability to plan execution for a specific date/time.

waitfor_code 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.

waitfordelay_code Results:waitfordelay_results

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s