0
IF ... ELSE Statements
Good morning everyone! Today's short blog post (as promised) is going to be about IF ... ELSE statements. IF ... ELSE statements are conditional, so their result is dependent on whether the IF part of the statement has been satisfied. If not, then control passes to the corresponding ELSE statement. Batches of these statements can be used, however make sure you limit how many are nested as it can have a negative impact on speed and query performance if you're not careful.
Personally, I have come across IF ... ELSE statements regularly in projects at work and have used them more than CASE statements - mainly because although their functionality is similar, IF ... ELSE statements are used to control the flow of transactions within a Stored Procedure, whereas CASE statements are not.
Personally, I have come across IF ... ELSE statements regularly in projects at work and have used them more than CASE statements - mainly because although their functionality is similar, IF ... ELSE statements are used to control the flow of transactions within a Stored Procedure, whereas CASE statements are not.
Simple IF ... ELSE Statement
An example of a simple IF ... ELSE statement is shown below. The variable Price is declared and given a value of £9.99. If the value of Price is less than £10.00, then display a message to the user to state that this is a sale item. If not, then display a message to the user to state that it is actually a full price item.IF ... ELSE Statement with SELECT condition
An example of an IF ... ELSE statement that relies on a result from a SELECT statement is shown below. If there are more than 2 assets in the database that are located in "Basildon", then display a message to the user to state that there are more than 2 assets in Basildon. If this is not the case however, display a message to the user stating that there are 2 or less assets in Basildon.
Nested IF... ELSE Statement
An example of a nested IF ... ELSE statement is shown below. This is taken from the example above and expanded so if there are fewer than 5 assets in Basildon, perform another check to see if there are any assets in Basildon at all. When nesting IF statements, you will need to surround each individual nested statement with a BEGIN ... END clause.
Overall
IF...ELSE statements can be flexible and used in many situations with a combination of different control flow statements, which I will explain in more detail (with examples) during the coming days.
What Next...
Next week, I will be writing more about the WHILE statement within SQL. Stay tuned! If you have any questions/comments/feedback, please leave them in the comments section below and I will get back to you as soon as I can. Alternatively, please like my SQL Genius Facebook Page and leave a message on there. Thanks for reading my blog post and thanks to my blog followers for all of your support on my journey! Have a great weekend everyone!
Post a Comment
Please post any feedback or comments here...