Handling errors in BTEQ with ACTIVITYCOUNT and ERRORCODE

By | 12th May 2020

Batch Teradata Query(BTEQ) is a powerful utility in Teradata with multiple options. You can export/import tables and files and make use of them in the same sessions for checking conditions to take decisions. Based on the checks, you can perform your DDL, DML operations. In this post, we will use a very simple use case of ACTIVITYCOUNT and ERRORCODE functions in Teradata.

The ACTIVITYCOUNT is to check the query that we are running is returning any result or not. It will simply tell you whether the query has returned zero or more records. With this conditional check, we will be able to execute subsequent statements.

The ERRORCODE is another useful function to check if there is any error while executing the statement. This along with ACTIVITYCOUNT will give the flexibility to handle the BTEQ scripts.

.LOGIN TeradataServer/username,password
SELECT 1 FROM custdb.customer where name='techieshouts';

.IF ERRORCODE > 0 THEN .EXIT 1;
.IF ACTIVITYCOUNT = 0 then EXIT 2;

EXIT 0;

In the above code, we are running a query to see if customer information is already there in the table. If the user information is available, the ACTIVITYCOUNT will not be zero. If it returns zero, the code will exit with code 2.

Meanwhile, if there is any issue while executing the SQL statement say if the logged-in user doesn’t have read access to the table or something, the ERRORCODE will not be zero. This will exit the BTEQ with the return code 1.

Also check “BTEQ export table to a file“, “If else in Teradata using BTEQ