How to use if/else statements in SQL

How to use if-else statements in SQL

The SQL query language supports if/else statements just like most programming languages, and in a similar fashion.

Example:

IF [Condition]
BEGIN
[Statement]
END
ELSE
BEGIN
[Statement]
END

The main difference is that instead of using the curly brackets – { and } – BEGIN and END satatements are being used.

Following the same flow, an if/else if/else statement looks like this:

IF [Condition]
BEGIN
[Statement]
END
ELSE IF
BEGIN
[Statement]
END
ELSE
BEGIN
[Statement]
END

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top