{"id":721,"date":"2020-07-08T08:36:47","date_gmt":"2020-07-08T03:06:47","guid":{"rendered":"https:\/\/techieshouts.com\/?p=721"},"modified":"2022-08-09T19:05:27","modified_gmt":"2022-08-09T13:35:27","slug":"if-else-checks-in-teradata-bteq","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/if-else-checks-in-teradata-bteq\/","title":{"rendered":"Conditional flow using simple If ELSE in BTEQ"},"content":{"rendered":"\n<p><strong>B<\/strong>asic <strong>TE<\/strong>radata <strong>Q<\/strong>uery utility is one of the advantages of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Teradata\" target=\"_blank\" rel=\"noopener\">Teradata<\/a> over other extraction utilities. The advantage of the BTEQ utility is its error handling ability and conditional execution. It is more common that the query execution is often compared against the conditional flow in programming languages and programmers may end up choosing JDBC connection to have control over the execution than writing scripts to achieve the same<\/p>\n\n\n\n<p>While there are no direct if-else conditional statements in BTEQ, we can still achieve that with the help of ACTIVITYCOUNT and GOTO statements.<\/p>\n\n\n\n<p>We have already seen the usage of ACTIVITYCOUNT in the other post. Now we will use GOTO along with that to perform conditional execution.<\/p>\n\n\n\n<h3>Conditional flow<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if(customer in customerdb)\n{\n    get his details;\n}\nelse\n{\n    insert into customerdb from another db;\n    export the customer information from newdb to a file;\n}<\/pre>\n\n\n\n<h3>Conditional flow using BTEQ<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">.LOGON teradataserver\/userid,password\n\nSELECT 1 from custdb.customer where name='techieshouts';\n.IF ERRORCODE > 0 THEN .EXIT 1;\n.IF ACTIVITYCOUNT = 0 then GOTO LOAD_INFO;\n\n.EXPORT file = &lt;output_path of file>\n.SET SEPARATOR '|'\n select name,city,country FROM custdb.customer where name='techieshouts';\n.EXPORT reset;\n.IF ERRORCODE > 0 THEN .EXIT 1;\n.IF ACTIVITYCOUNT = 0 then .EXIT 2;\n\n.LABEL LOAD_INFO;\nINSERT INTO custdb.customer \nSELECT name,city,country FROM oldcustomerdb.customer;\n\n.EXPORT file = &lt;output_path of file>\n.SET SEPARATOR '|'\n SELECT name,city,country FROM custdb.customer where name='techieshouts';\n.EXPORT reset;\n.IF ERRORCODE > 0 THEN .EXIT 1;\n.IF ACTIVITYCOUNT = 0 then .EXIT 2;\n\n.LOGOFF\nEXIT 0;<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Let us see the purpose of each of the above statements.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT 1 FROM custdb.customer WHERE name='techieshouts';\n.IF ERRORCODE > 0 THEN .EXIT 1;\n.IF ACTIVITYCOUNT = 0 then GOTO LOAD_INFO;<\/pre>\n\n\n\n<p>In the above statement, first, we are selecting the customer information from the customer database. The ACTIVITYCOUNT here will tell if the record is already available or not. If the value is zero then we are sending the control to the label &#8220;LOAD_INFO&#8221;. This is the successful case of If condition. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">.LABEL LOAD_INFO;\nINSERT INTO custdb.customer \nSELECT name,city,country FROM oldcustomerdb.customer;\n.EXPORT file = &lt;output path>\n.SET SEPARATOR '|'\nSELECT name,city,country FROM custdb.customer WHERE  name='techieshouts';\n.EXPORT reset;\n.IF ERRORCODE > 0 THEN .EXIT 1;\n.IF ACTIVITYCOUNT = 0 then .EXIT 2;<\/pre>\n\n\n\n<p>Here inside the If successful case, we are first inserting the data into the customerdb from the oldcustomerdb. Next, we are exporting the information from the customerdb to a file. After that, we are checking the ERRORCODE and ACTIVITYCOUNT to make sure the export is successful.<\/p>\n\n\n\n<p>The else part here is the statements after the first select and before the .LABEL LOAD_INFO.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">.EXPORT file = &lt;output path>\n.SET SEPARATOR '|'\nSELECT name,city,country FROM custdb.customer WHERE name='techieshouts';\n.EXPORT reset;\n.IF ERRORCODE > 0 THEN .EXIT 1;\n.IF ACTIVITYCOUNT = 0 then .EXIT 2;<\/pre>\n\n\n\n<p>This is the same as what we saw above. The regular flow to export the data to a file. The numbers given after the .EXIT keyword is the return code the will be given as the return code for the wrapper script<\/p>\n\n\n\n<p>Finally, the flow is completed with the .logoff statement.<\/p>\n\n\n\n<p>Also, check <a href=\"https:\/\/techieshouts.com\/activitycount-and-errorcode-in-teradata-bteq\/\">&#8220;Error handling with ACTIVITYCOUNT and ERRORCODE in BTEQ&#8221;<\/a>, &#8220;<a href=\"https:\/\/techieshouts.com\/bteq-export-without-header\/\">BTEQ export to file without column header<\/a>&#8220;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Basic TEradata Query utility is one of the advantages of Teradata over other extraction utilities. The advantage of the BTEQ utility is its error handling ability and conditional execution. It is more common that the query execution is often compared against the conditional flow in programming languages and programmers may end up choosing JDBC connection\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/if-else-checks-in-teradata-bteq\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,9],"tags":[99,78,164,98,83,79,85,185,77],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/721"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=721"}],"version-history":[{"count":42,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"predecessor-version":[{"id":1180,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/721\/revisions\/1180"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}