{"id":5803,"date":"2013-06-21T00:00:00","date_gmt":"2013-06-21T00:00:00","guid":{"rendered":"http:\/\/craftydba.com\/?p=5803"},"modified":"2016-04-22T12:42:41","modified_gmt":"2016-04-22T12:42:41","slug":"controlling-the-flow-part-1","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5803","title":{"rendered":"Controlling The Flow &#8211; Part 1"},"content":{"rendered":"<p>I am going to write a few articles (tidbits) on controlling the execution flow of statements in a Transaction SQL (T-SQL) program.  <\/p>\n<p>Every program has to be deterministic on what step to execute next.  Otherwise, the computer would be clueless on what to do next.<\/p>\n<p>The only exception to this rule is error handling.  A runtime error may cause the program to branch to a error handler and exit the program.<\/p>\n<p>Today, I want to talk about looping for repetition, statement blocks, and conditional execution.<\/p>\n<p>Some programming languages support the following constructs: <\/p>\n<ol>\n<li>FOR LOOP<\/li>\n<li>DO WHILE LOOP<\/li>\n<li>LOOP UNTIL<\/li>\n<\/ol>\n<p>Microsoft choose to implement the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms178642.aspx\">WHILE<\/a> construct in Transaction SQL (T-SQL).  Basically, the program repeats the execution of a statement or statement block until a condition is false.<\/p>\n<p>Statement blocks are created by wrapping code between the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms190487.aspx\">BEGIN<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms176030.aspx\">END<\/a> keywords.  The block is executed as one set of statements.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms182717.aspx\">IF<\/a> statement directs the program to execute a statement only if a condition is true.  Any false conditions causes the statements to be skipped.<\/p>\n<p>The example below creates a WHILE loop that counts from 1 to 100.  The key part of the code is to increment the counter.  Else, the program will become an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Infinite_loop\">infinite loop<\/a>.<\/p>\n<p>The IF statement is inside a statement block.  It only displays (<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms176047.aspx\">PRINT<\/a>) numbers that are divisible by three.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"while statement - controlling the flow\">\r\n--\r\n-- Show numbers divisible by 3\r\n--\r\n\r\n-- Loop counter\r\nDECLARE @VAR_CNT INT = 1;\r\n\r\n-- Count to 100\r\nWHILE (@VAR_CNT <= 100)\r\nBEGIN\r\n    -- Print when modulo is zero\r\n    IF (@VAR_CNT % 3) = 0\r\n        PRINT @VAR_CNT;\r\n\r\n    -- Increment the counter\r\n    SET @VAR_CNT += 1;\r\nEND;\r\nGO\r\n<\/pre>\n<\/p>\n<p>The output of this program is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\n3\r\n6\r\n\r\n(...)\r\n\r\n96\r\n99\r\n\r\n<\/pre>\n<\/p>\n<p>The long and short of it, most Transaction SQL (T-SQL) programs will use <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms174290.aspx\">Control Flow Language<\/a> statements to provide direction to the database transaction engine.  <\/p>\n<p>The WHILE statement combined with a statement block (BEGIN\/END) allows a database developer to perform a operation a number of times.  <\/p>\n<p>The IF statement can be used to conditionally execute a statement.  These are just some of the building blocks to write your next program.  <\/p>\n<p>Next time, I will be talking about the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms187331.aspx\">WAITFOR<\/a> statement that was introduced in Microsoft SQL Server 2005.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going to write a few articles (tidbits) on controlling the execution flow of statements in a Transaction SQL (T-SQL) program. Every program has to be deterministic on what step to execute next. Otherwise, the computer would be clueless on what to do next. The only exception to this rule is error handling. A runtime error may cause the program to branch to a error handler and exit the program. Today, I want to talk about looping for repetition, statement blocks, and conditional execution. Some programming languages support the&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5806,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[322,870,31,871,321,15,872,323,28,29,339],"class_list":["post-5803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-very-short-articles","tag-begin","tag-control-flow-language","tag-database-developer","tag-end","tag-if","tag-john-f-miner-iii","tag-looping","tag-print","tag-sql-server","tag-tsql","tag-while"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5803"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5803\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/media\/5806"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}