Alternatives · Basic Logics · Real Scenario Logic's

Common Alternatives to IF ELSE Logic in SQL Server with Simple Example in SQL Server-[PART 2]

Introduction-

This is a Part 2 of one of my previous post named -‘Common Alternatives to IF ELSE Logic in SQL Server with Simple Example‘.

In Part 1, we have already seen what are the common alternatives of IF ELSE logic in SQL Server and what are the Definition,Syntax and Demo SQL Scripts for IF ELSE,CASE Expressions and IIF Functions with Simple Example.

In this post (Part 2), we are going to see what happens to the Output if we skip ELSE Part in IF ELSE Logic’s , CASE Expressions and IIF Function in SQL Server.

IF ELSE LOGIC

If we skip ELSE Part Logic in IF ELSE–> Messages Tab would only come in Results pane with Message named ‘ Commands completed successfully. ‘ and Results Tab wont come.

SQL QUERY WITH SIMPLE EXAMPLE

--IF ELSE LOGIC
IF 1=0
    SELECT 'True' AS RESULT;
OUTPUT – Commands completed successfully.

CASE EXPRESSION

If we skip ELSE Part Logic in CASE Expression–> Results Tab(Value as NULL) will come along with Messages Tab.

SQL QUERY WITH SIMPLE EXAMPLE

--CASE EXPRESSION
SELECT
    CASE
        WHEN 1=0 
            THEN 'True'
    END AS RESULT;
OUTPUT – Results Tab(Value as NULL) will come along with Messages Tab

IIF FUNCTION

If we skip ELSE Part Logic in IIF Function–> Messages Tab would only come in Results pane with Message named ‘ Msg 102, Level 15, State 1, Line 12
Incorrect syntax near ‘)’.
‘ and Results Tab wont come.

SQL QUERY WITH SIMPLE EXAMPLE

--IIF FUNCTION
SELECT IIF( 1=0 ,'True' ) AS RESULT;
OUTPUT – Msg 102, Level 15, State 1, Line 12
Incorrect syntax near ‘)’. in Message Tab.

NOTE –

If we skip ELSE Logic for IIF Function , the syntax for it becomes wrong and so end up with error.The Red Line(IntelliSense) that highlighted in above image shows the syntax itself is wrong. As per the IIF Function’s Syntax, we must need to add ELSE Part while using it. Also, the Line Number that is highlighted in the above image shows the particular line where the error occurs.These errors may look like simple but plays a major role in next layers like API etc., So we should add ELSE Logic’s always, while using IF ELSE, CASE Expressions and IIF Functions in SQL Server to avoid above Such errors.

Conclusion

In this blog post, we saw the outputs of ELSE Part skipping Logics in IF ELSE, CASE Expressions and IIF Function at SQL Server.

Thanks for reading!

FOLLOW BLOG for interesting upcoming posts!

Advertisement

One thought on “Common Alternatives to IF ELSE Logic in SQL Server with Simple Example in SQL Server-[PART 2]

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