{"id":149,"date":"2019-09-24T07:53:34","date_gmt":"2019-09-24T07:53:34","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=149"},"modified":"2019-09-24T08:03:46","modified_gmt":"2019-09-24T08:03:46","slug":"if-else","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2019\/09\/24\/if-else\/","title":{"rendered":"IF Else"},"content":{"rendered":"\n<p>The&nbsp;<code>IF...ELSE<\/code>&nbsp;statement&nbsp;is a control-flow statement that allows you to execute or skip a&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-begin-end\/\">statement block<\/a>&nbsp;based on a specified condition.<\/p>\n\n\n\n<h2>&nbsp;<code>IF<\/code>&nbsp;statement<\/h2>\n\n\n\n<p>The following illustrates the syntax of the&nbsp;<code>IF<\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234<\/td><td>IF boolean_expression&nbsp;&nbsp; BEGIN&nbsp;&nbsp;&nbsp;&nbsp;{ statement_block }END<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this syntax, if the&nbsp;<code>Boolean_expression<\/code>&nbsp;evaluates to&nbsp;<code>TRUE<\/code>&nbsp;then the&nbsp;<code>statement_block<\/code>&nbsp;in the&nbsp;<code>BEGIN...END<\/code>&nbsp;block is executed. Otherwise, the&nbsp;<code>statement_block<\/code>&nbsp;is skipped and the control of the&nbsp;program is passed to the statement after the&nbsp;<code>END<\/code>&nbsp;keyword.<\/p>\n\n\n\n<p>Note that if the Boolean expression contains a&nbsp;<code>SELECT<\/code>&nbsp;statement, you must enclose the&nbsp;<code>SELECT<\/code>&nbsp;statement in parentheses.<\/p>\n\n\n\n<p>The following example first gets the sales amount from the&nbsp;<code>sales.order_items<\/code>&nbsp;table in the&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-sample-database\/\">sample database<\/a>. Then, it prints out a message if the sales amount is greater than 1 million.<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123456789101112131415161718<\/td><td>BEGIN&nbsp;&nbsp;&nbsp;&nbsp;DECLARE @sales INT;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@sales = SUM(list_price * quantity)&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sales.order_items i&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INNER JOIN sales.orders o ON o.order_id = i.order_id&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YEAR(order_date) = 2018;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT @sales;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF @sales &gt; 1000000&nbsp;&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT &#8216;Great! The sales amount in 2018 is greater than 1,000,000&#8217;;&nbsp;&nbsp;&nbsp;&nbsp;ENDEND<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The output of the code block is:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>Great! The sales amount in 2018 is greater than 1,000,000<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Note that you have to click the&nbsp;<strong>Messages<\/strong>&nbsp;tab to see the above output message:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-if-else-output.png\" alt=\"\" class=\"wp-image-2051\"\/><\/figure>\n\n\n\n<h2>&nbsp;<code>IF ELSE<\/code>&nbsp;statement<\/h2>\n\n\n\n<p>When the condition in the&nbsp;<code>IF<\/code>&nbsp;clause evaluates to&nbsp;<code>FALSE<\/code>&nbsp;and you want to execute another statement block, you can use the&nbsp;<code>ELSE<\/code>&nbsp;clause.<\/p>\n\n\n\n<p>The following illustrates the&nbsp;<code>IF ELSE<\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12345678<\/td><td>IF Boolean_expressionBEGIN&nbsp;&nbsp;&nbsp;&nbsp;<em>&#8212; Statement block executes when the Boolean expression is TRUE<\/em>ENDELSEBEGIN&nbsp;&nbsp;&nbsp;&nbsp;<em>&#8212; Statement block executes when the Boolean expression is FALSE<\/em>END<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Each&nbsp;<code>IF<\/code>&nbsp;statement has a condition. If the condition evaluates to&nbsp;<code>TRUE<\/code>&nbsp;then the statement block in the&nbsp;<code>IF<\/code>&nbsp;clause is executed. If the condition is&nbsp;<code>FALSE<\/code>, then the code block in the&nbsp;<code>ELSE<\/code>&nbsp;clause is executed.<\/p>\n\n\n\n<p>See the following example:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12345678910111213141516171819202122<\/td><td>BEGIN&nbsp;&nbsp;&nbsp;&nbsp;DECLARE @sales INT;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@sales = SUM(list_price * quantity)&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sales.order_items i&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INNER JOIN sales.orders o ON o.order_id = i.order_id&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YEAR(order_date) = 2017;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT @sales;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF @sales &gt; 10000000&nbsp;&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT &#8216;Great! The sales amount in 2018 is greater than 10,000,000&#8217;;&nbsp;&nbsp;&nbsp;&nbsp;END&nbsp;&nbsp;&nbsp;&nbsp;ELSE&nbsp;&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT &#8216;Sales amount in 2017 did not reach 10,000,000&#8217;;&nbsp;&nbsp;&nbsp;&nbsp;ENDEND<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this example,<\/p>\n\n\n\n<p>First, the following statement set the total sales in 2017 to the&nbsp;<code>@sales<\/code>&nbsp;variable:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567<\/td><td>&nbsp;&nbsp;&nbsp;&nbsp;SELECT &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@sales = SUM(list_price * quantity)&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sales.order_items i&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INNER JOIN sales.orders o ON o.order_id = i.order_id&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YEAR(order_date) = 2017;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Second, this statement returns the sales to the output:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>&nbsp;&nbsp;&nbsp;&nbsp;SELECT @sales;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Finally, the&nbsp;<code>IF<\/code>&nbsp;clause checks if the sales amount in 2017 is greater than 10 million. Because the sales amount is less than that, the statement block in the&nbsp;<code>ELSE<\/code>&nbsp;clause executes.<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12345678<\/td><td>&nbsp;&nbsp;&nbsp;&nbsp;IF @sales &gt; 10000000&nbsp;&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT &#8216;Great! The sales amount in 2018 is greater than 10,000,000&#8217;;&nbsp;&nbsp;&nbsp;&nbsp;END&nbsp;&nbsp;&nbsp;&nbsp;ELSE&nbsp;&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT &#8216;Sales amount in 2017 did not reach 10,000,000&#8217;;&nbsp;&nbsp;&nbsp;&nbsp;END<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The following shows the output:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>Sales amount did not reach 10,000,000<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h3>Nested&nbsp;<code>IF...ELSE<\/code><\/h3>\n\n\n\n<p>SQL Server allows you to nest an&nbsp;<code>IF...ELSE<\/code>&nbsp;statement within inside another&nbsp;<code>IF...ELSE<\/code>&nbsp;statement, see the following example:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123456789101112<\/td><td>BEGIN&nbsp;&nbsp;&nbsp;&nbsp;DECLARE @x INT = 10,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@y INT = 20;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF (@x &gt; 0)&nbsp;&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF (@x &lt; @y)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT &#8216;x &gt; 0 and x &lt; y&#8217;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ELSE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT &#8216;x &gt; 0 and x &gt;= y&#8217;;&nbsp;&nbsp;&nbsp;&nbsp;END END<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<p>First, declare two variables @x and @y and set their values to 10 and 20 respectively:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12<\/td><td>DECLARE @x INT = 10,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@y INT = 20;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Second, the output&nbsp;<code>IF<\/code>&nbsp;statement check if&nbsp;<code>@x<\/code>&nbsp;is greater than zero. Because&nbsp;<code>@x<\/code>&nbsp;is set to 10, the condition (<code>@x &gt; 10<\/code>) is true. Therefore, the nested&nbsp;<code>IF<\/code>&nbsp;statement executes.<\/p>\n\n\n\n<p>Finally, the nested&nbsp;<code>IF<\/code>&nbsp;statement check if @x is less than @y (&nbsp;<code>@x &lt; @y<\/code>). Because&nbsp;<code>@y<\/code>&nbsp;is set to 20,&nbsp; the condition (<code>@x &lt; @y<\/code>) evaluates to true. The&nbsp;<code>PRINT 'x &gt; 0 and x &lt; y';<\/code>&nbsp;statement in the&nbsp;<code>IF<\/code>&nbsp;branch executes.<\/p>\n\n\n\n<p>Here is the output:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>x &gt; 0 and x &lt; y<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>It is a good practice to not nest an&nbsp;<code>IF<\/code>&nbsp;statement inside another statement because it makes the code difficult to read and hard to maintain.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;IF&#8230;ELSE&nbsp;statement&nbsp;is a control-flow statement that allows you to execute or skip a&nbsp;statement block&nbsp;based on a specified condition. &nbsp;IF&nbsp;statement The following illustrates the syntax of the&nbsp;IF&nbsp;statement: 1234 IF boolean_expression&nbsp;&nbsp; BEGIN&nbsp;&nbsp;&nbsp;&nbsp;{ statement_block }END In this syntax, if&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,18,19],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/149"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=149"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/149\/revisions"}],"predecessor-version":[{"id":150,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/149\/revisions\/150"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}