{"id":147,"date":"2019-09-24T07:52:53","date_gmt":"2019-09-24T07:52:53","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=147"},"modified":"2019-09-24T08:03:46","modified_gmt":"2019-09-24T08:03:46","slug":"begin-end","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2019\/09\/24\/begin-end\/","title":{"rendered":"Begin END"},"content":{"rendered":"\n<h2>Overview of the&nbsp;<code>BEGIN...END<\/code>&nbsp;Statement<\/h2>\n\n\n\n<p>The&nbsp;<code>BEGIN...END<\/code>&nbsp;statement is used to define a statement block. A statement block consists of a set of SQL statements that execute together. A statement block is also known as a batch.<\/p>\n\n\n\n<p>In other words, if statements are sentences, the&nbsp;<code>BEGIN...END<\/code>&nbsp;statement allows you to define paragraphs.<\/p>\n\n\n\n<p>The following illustrates the syntax of the&nbsp;<code>BEGIN...END<\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123<\/td><td>BEGIN&nbsp;&nbsp;&nbsp;&nbsp;{ sql_statement | statement_block}END<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this syntax, you place a set of SQL statements between the&nbsp;<code>BEGIN<\/code>&nbsp;and&nbsp;<code>END<\/code>&nbsp;keywords, for example:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123456789101112<\/td><td>BEGIN&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_id,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;production.products&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price &gt; 100000;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF @@ROWCOUNT=&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT &#8216;No product with price greater than 100000 found&#8217;;END<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul><li>First, we have a block starting with the&nbsp;<code>BEGIN<\/code>&nbsp;keyword and ending with the&nbsp;<code>END<\/code><br>keyword.<\/li><li>Second, inside the block, we have a&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\">SELECT<\/a><\/code>&nbsp;statement that finds products whose list prices are greater than 100,000. Then, we have the&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/\">IF<\/a><\/code>&nbsp;statement to check if the query returns any product and print out a message if no product returns.<\/li><\/ul>\n\n\n\n<p>The&nbsp;<code>BEGIN... END<\/code>&nbsp;statement bounds a logical block of SQL statements. We often use the&nbsp;<code>BEGIN...END<\/code>&nbsp;at the start and end of a&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/\">stored procedure<\/a>&nbsp;and&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/\">function<\/a>. But it is not strictly necessary.<\/p>\n\n\n\n<p>However, the&nbsp;<code>BEGIN...END<\/code>&nbsp;is required for the&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/\">IF ELSE<\/a><\/code>&nbsp;statements,&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/\">WHILE<\/a><\/code>&nbsp;statements, etc., where you need to wrap multiple statements.<\/p>\n\n\n\n<h2>Nesting&nbsp;<code>BEGIN... END<\/code><\/h2>\n\n\n\n<p>The statement block can be nested. It simply means that you can place a&nbsp;<code>BEGIN...END<\/code>&nbsp;statement within another&nbsp;<code>BEGIN... END<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>Consider the following example:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12345678910111213141516171819<\/td><td>BEGIN&nbsp;&nbsp;&nbsp;&nbsp;DECLARE @name VARCHAR(MAX);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT TOP 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@name = product_name&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;production.products&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price DESC;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF @@ROWCOUNT &lt;&gt; 0&nbsp;&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT &#8216;The most expensive product is &#8216; + @name&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;No product found&#8217;;&nbsp;&nbsp;&nbsp;&nbsp;END;END<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this example, we used the&nbsp;<code>BEGIN...END<\/code>&nbsp;statement to wrap the whole statement block. Inside this block, we also used the&nbsp;<code>BEGIN...END<\/code>&nbsp;for the&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/\">IF...ELSE<\/a><\/code>&nbsp;statement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview of the&nbsp;BEGIN&#8230;END&nbsp;Statement The&nbsp;BEGIN&#8230;END&nbsp;statement is used to define a statement block. A statement block consists of a set of SQL statements that execute together. A statement block is also known as a batch. In other&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\/147"}],"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=147"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":148,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions\/148"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}