{"id":830,"date":"2018-10-03T14:17:40","date_gmt":"2018-10-03T07:17:40","guid":{"rendered":"http:\/\/www.sqlservertutorial.net\/?page_id=830"},"modified":"2020-07-13T07:29:49","modified_gmt":"2020-07-13T00:29:49","slug":"sql-server-if-else","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/","title":{"rendered":"SQL Server IF ELSE"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn SQL Server <code>IF...ELSE<\/code> statement to control the flow of program.<\/p>\n\n\n\n<p>The <code>IF...ELSE<\/code> statement&nbsp;is a control-flow statement that allows you to execute or skip a <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-begin-end\/\">statement block<\/a> based on a specified condition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-if-statement'>The IF statement <a href=\"#the-if-statement\" class=\"anchor\" id=\"the-if-statement\" title=\"Anchor for The &lt;code&gt;IF&lt;\/code&gt; statement\">#<\/a><\/h2>\n\n\n\n<p>The following illustrates the syntax of the <code>IF<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">IF boolean_expression   \n<span class=\"hljs-keyword\">BEGIN<\/span>\n    { statement_block }\n<span class=\"hljs-keyword\">END<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, if the <code>Boolean_expression<\/code> evaluates to <code>TRUE<\/code> then the <code>statement_block<\/code> in the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-begin-end\/\">BEGIN...END<\/a><\/code> block is executed. Otherwise, the <code>statement_block<\/code> is skipped and the control of the&nbsp;program is passed to the statement after the <code>END<\/code> keyword.<\/p>\n\n\n\n<p>Note that if the Boolean expression contains a <code>SELECT<\/code> statement, you must enclose the <code>SELECT<\/code> statement in parentheses.<\/p>\n\n\n\n<p>The following example first gets the sales amount from the <code>sales.order_items<\/code> table in the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-sample-database\/\">sample database<\/a> and then prints out a message if the sales amount is greater than 1 million.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">DECLARE<\/span> @sales <span class=\"hljs-built_in\">INT<\/span>;\n\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        @sales = <span class=\"hljs-keyword\">SUM<\/span>(list_price * quantity)\n    <span class=\"hljs-keyword\">FROM<\/span>\n        sales.order_items i\n        <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> sales.orders o <span class=\"hljs-keyword\">ON<\/span> o.order_id = i.order_id\n    <span class=\"hljs-keyword\">WHERE<\/span>\n        <span class=\"hljs-keyword\">YEAR<\/span>(order_date) = <span class=\"hljs-number\">2018<\/span>;\n\n    <span class=\"hljs-keyword\">SELECT<\/span> @sales;\n\n    IF @sales &gt; 1000000\n    <span class=\"hljs-keyword\">BEGIN<\/span>\n        PRINT <span class=\"hljs-string\">'Great! The sales amount in 2018 is greater than 1,000,000'<\/span>;\n    <span class=\"hljs-keyword\">END<\/span>\n<span class=\"hljs-keyword\">END<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output of the code block is:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">Great! The sales amount in 2018 is greater than 1,000,000\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that you have to click the <strong>Messages<\/strong> tab to see the above output message:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"320\" height=\"79\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-if-else-output.png\" alt=\"\" class=\"wp-image-2051\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-if-else-output.png 320w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-if-else-output-300x74.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/figure>\n\n\n\n\n\n\n<h2 class=\"wp-block-heading\" id='the-if-else-statement'>The IF ELSE statement <a href=\"#the-if-else-statement\" class=\"anchor\" id=\"the-if-else-statement\" title=\"Anchor for The &lt;code&gt;IF ELSE&lt;\/code&gt; statement\">#<\/a><\/h2>\n\n\n\n<p>When the condition in the <code>IF<\/code> clause evaluates to <code>FALSE<\/code> and you want to execute another statement block, you can use the <code>ELSE<\/code> clause.<\/p>\n\n\n\n<p>The following illustrates the <code>IF ELSE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">IF Boolean_expression\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-comment\">-- Statement block executes when the Boolean expression is TRUE<\/span>\n<span class=\"hljs-keyword\">END<\/span>\n<span class=\"hljs-keyword\">ELSE<\/span>\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-comment\">-- Statement block executes when the Boolean expression is FALSE<\/span>\n<span class=\"hljs-keyword\">END<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Each <code>IF<\/code> statement has a condition. If the condition evaluates to <code>TRUE<\/code> then the statement block in the <code>IF<\/code> clause is executed. If the condition is <code>FALSE<\/code>, then the code block in the <code>ELSE<\/code> clause is executed.<\/p>\n\n\n\n<p>See the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">DECLARE<\/span> @sales <span class=\"hljs-built_in\">INT<\/span>;\n\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        @sales = <span class=\"hljs-keyword\">SUM<\/span>(list_price * quantity)\n    <span class=\"hljs-keyword\">FROM<\/span>\n        sales.order_items i\n        <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> sales.orders o <span class=\"hljs-keyword\">ON<\/span> o.order_id = i.order_id\n    <span class=\"hljs-keyword\">WHERE<\/span>\n        <span class=\"hljs-keyword\">YEAR<\/span>(order_date) = <span class=\"hljs-number\">2017<\/span>;\n\n    <span class=\"hljs-keyword\">SELECT<\/span> @sales;\n\n    IF @sales &gt; 10000000\n    <span class=\"hljs-keyword\">BEGIN<\/span>\n        PRINT <span class=\"hljs-string\">'Great! The sales amount in 2018 is greater than 10,000,000'<\/span>;\n    <span class=\"hljs-keyword\">END<\/span>\n    <span class=\"hljs-keyword\">ELSE<\/span>\n    <span class=\"hljs-keyword\">BEGIN<\/span>\n        PRINT <span class=\"hljs-string\">'Sales amount in 2017 did not reach 10,000,000'<\/span>;\n    <span class=\"hljs-keyword\">END<\/span>\n<span class=\"hljs-keyword\">END<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example:<\/p>\n\n\n\n<p>First, the following statement sets the total sales in 2017 to the <code>@sales<\/code> variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">    <span class=\"hljs-keyword\">SELECT<\/span> \n        @sales = <span class=\"hljs-keyword\">SUM<\/span>(list_price * quantity)\n    <span class=\"hljs-keyword\">FROM<\/span>\n        sales.order_items i\n        <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> sales.orders o <span class=\"hljs-keyword\">ON<\/span> o.order_id = i.order_id\n    <span class=\"hljs-keyword\">WHERE<\/span>\n        <span class=\"hljs-keyword\">YEAR<\/span>(order_date) = <span class=\"hljs-number\">2017<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, this statement returns the sales to the output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">    <span class=\"hljs-keyword\">SELECT<\/span> @sales;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, the <code>IF<\/code> 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 <code>ELSE<\/code> clause executes.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">    IF @sales &gt; 10000000\n    <span class=\"hljs-keyword\">BEGIN<\/span>\n        PRINT <span class=\"hljs-string\">'Great! The sales amount in 2018 is greater than 10,000,000'<\/span>;\n    <span class=\"hljs-keyword\">END<\/span>\n    <span class=\"hljs-keyword\">ELSE<\/span>\n    <span class=\"hljs-keyword\">BEGIN<\/span>\n        PRINT <span class=\"hljs-string\">'Sales amount in 2017 did not reach 10,000,000'<\/span>;\n    <span class=\"hljs-keyword\">END<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following shows the output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">Sales amount did not reach 10,000,000\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='nested-if-else'>Nested IF...ELSE <a href=\"#nested-if-else\" class=\"anchor\" id=\"nested-if-else\" title=\"Anchor for Nested &lt;code&gt;IF...ELSE&lt;\/code&gt;\">#<\/a><\/h3>\n\n\n\n<p>SQL Server allows you to nest an <code>IF...ELSE<\/code> statement within inside another <code>IF...ELSE<\/code> statement, see the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">DECLARE<\/span> @x <span class=\"hljs-built_in\">INT<\/span> = <span class=\"hljs-number\">10<\/span>,\n            @y <span class=\"hljs-built_in\">INT<\/span> = <span class=\"hljs-number\">20<\/span>;\n\n    IF (@x &gt; 0)\n    <span class=\"hljs-keyword\">BEGIN<\/span>\n        <span class=\"hljs-keyword\">IF<\/span> (@x &lt; @y)\n            PRINT <span class=\"hljs-string\">'x &gt; 0 and x &lt; y'<\/span>;\n        ELSE\n            PRINT 'x &gt; 0 and x &gt;= y';\n    <span class=\"hljs-keyword\">END<\/span>\t\t\t\n<span class=\"hljs-keyword\">END<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\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<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DECLARE<\/span> @x <span class=\"hljs-built_in\">INT<\/span> = <span class=\"hljs-number\">10<\/span>,\n        @y <span class=\"hljs-built_in\">INT<\/span> = <span class=\"hljs-number\">20<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, the output <code>IF<\/code> statement check if <code>@x<\/code> is greater than zero. Because <code>@x<\/code> is set to 10, the condition (<code>@x &gt; 10<\/code>) is true. Therefore, the nested <code>IF<\/code> statement executes.<\/p>\n\n\n\n<p>Finally, the nested <code>IF<\/code> statement check if @x is less than @y ( <code>@x &lt; @y<\/code>). Because <code>@y<\/code> is set to 20,&nbsp; the condition (<code>@x &lt; @y<\/code>) evaluates to true. The <code>PRINT 'x &gt; 0 and x &lt; y';<\/code> statement in the <code>IF<\/code> branch executes.<\/p>\n\n\n\n<p>Here is the output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">x &gt; 0 and x &lt; y<\/code><\/span><\/pre>\n\n\n<p>It is a good practice to not nest an <code>IF<\/code> statement inside another statement because it makes the code difficult to read and hard to maintain.<\/p>\n\n\n\n<p>In this tutorial, you have learned how to use the SQL Server <code>IF...ELSE<\/code> statement to control the flow of code execution.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"830\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/\"\n\t\t\t\tdata-post-title=\"SQL Server IF ELSE\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"830\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/\"\n\t\t\t\tdata-post-title=\"SQL Server IF ELSE\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use the SQL Server IF&#8230;ELSE statement to control the flow of a program.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":777,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-830","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server IF ELSE Statement By Examples<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the SQL Server IF...ELSE statement to control the flow of a program.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server IF ELSE Statement By Examples\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the SQL Server IF...ELSE statement to control the flow of a program.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2020-07-13T00:29:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-if-else-output.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-if-else\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-if-else\\\/\",\"name\":\"SQL Server IF ELSE Statement By Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-if-else\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-if-else\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/sql-server-if-else-output.png\",\"datePublished\":\"2018-10-03T07:17:40+00:00\",\"dateModified\":\"2020-07-13T00:29:49+00:00\",\"description\":\"This tutorial shows you how to use the SQL Server IF...ELSE statement to control the flow of a program.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-if-else\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-if-else\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-if-else\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/sql-server-if-else-output.png\",\"contentUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/sql-server-if-else-output.png\",\"width\":320,\"height\":79},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-if-else\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server Stored Procedures\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Server IF ELSE\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server IF ELSE Statement By Examples","description":"This tutorial shows you how to use the SQL Server IF...ELSE statement to control the flow of a program.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server IF ELSE Statement By Examples","og_description":"This tutorial shows you how to use the SQL Server IF...ELSE statement to control the flow of a program.","og_url":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2020-07-13T00:29:49+00:00","og_image":[{"url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-if-else-output.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/","url":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/","name":"SQL Server IF ELSE Statement By Examples","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-if-else-output.png","datePublished":"2018-10-03T07:17:40+00:00","dateModified":"2020-07-13T00:29:49+00:00","description":"This tutorial shows you how to use the SQL Server IF...ELSE statement to control the flow of a program.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/#primaryimage","url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-if-else-output.png","contentUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-if-else-output.png","width":320,"height":79},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQL Server Stored Procedures","item":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/"},{"@type":"ListItem","position":3,"name":"SQL Server IF ELSE"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=830"}],"version-history":[{"count":2,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/830\/revisions"}],"predecessor-version":[{"id":2675,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/830\/revisions\/2675"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/777"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}