Using IF Statements With T-SQL

in #data3 years ago

In the video T-SQL: How To Use If Statements, we look at how to use IF statements with T-SQL. These can be an alternative to CASE WHEN statements in some situations (see video T-SQL: How To Use CASE WHEN Statements) and they may be more intuitive for some developers who come from other backgrounds that use IF statements (most languages do not have CASE WHEN statements). I would not use IF statements in bucket items, as CASE WHEN statements will be more helpful there.

We see examples of:

  • Nested IF statements - 2 and 3deep
  • How to structure IF statements
  • Format of IF statements
  • Along with what do we want to consider about creating IF statements and troubleshooting?
We shouldn't overlook the last point - we tend to spend more time troubleshooting than developing, so we want to ensure that our code is readable to make debugging faster.

Automating ETL
Check out the highest-rated Automating ETL course on Udemy, if you're interested in data.

We also see that like other languages, we can nest IF statements. Even though we can do this, we should always consider readability when we do this. Outside of intentional convoluted coding, nesting many IF statements can lead to unreadable code - which may not be desired in our context (exceptions exist). If you do need to use IF statements, consider using appropriate indentations, as using multiple IF statements in one line may be far less readable. In addition, we may want to consider using variables for some of the validation.

YouTube | Automating ETL | T-SQL In 2 Hours | Consumer Guide To Digital Security