IF...THEN... - Basic Teradata Query

Basic Teradata Query Reference

Product
Basic Teradata Query
Release Number
15.10
Language
English (United States)
Last Update
2018-10-07
dita:id
B035-2414
lifecycle
previous
Product Category
Teradata Tools and Utilities

Purpose  

Tests the validity of the condition stated in the IF clause.

 

Condition Status

Result

True

BTEQ executes the action stated in the THEN clause.

False

BTEQ does not process the action in the THEN clause and proceeds to the next command statement.

Syntax  

where:

 

Syntax Element

Description

bteq_command
or
sql_request

BTEQ command or Teradata SQL statement to be executed when the IF condition is true.

n

A number in any of the following ranges:

  • 0 to 9000, inclusive, if specifying the ERRORCODE option.
  • 0 to 4,294,967,295 if specifying the ACTIVITYCOUNT option while connected to a 14.00 (or earlier) database. The range increases to 18,446,744,073,709,551,615 if connected to a 14.10+ database.
  • Or, if specifying the ERRORLEVEL option, n is an assigned error level number that can be 0, 2, 4, 6, 8, or higher.

    operator

    One of the following comparison operators:

    = (equal to)
    > (greater than)
    < (less than)
    >= (greater than or equal to)
    <= (less than or equal to)
    <> (not equal)
    != (not equal)
    ~= (not equal)
    ^= (not equal)

    Usage Notes  

     

    Specified Option

    Results

    ERRORCODE

    BTEQ substitutes for ERRORCODE the value of the error code that was generated by the last Teradata SQL request.

     

    If the last request completed successfully, the value of ERRORCODE is zero.

    ERRORLEVEL

    BTEQ returns the severity level code associated with that error level.

    ACTIVITYCOUNT

    BTEQ substitutes for ACTIVITYCOUNT the value of the number of rows affected by the last Teradata SQL statement. For a request resulting in a selection of rows, this is the number of rows returned to BTEQ from the database.

    The IF...THEN... command is valid in a Teradata SQL macro, if the THEN clause includes a valid BTEQ command that is also valid in a Teradata SQL macro.

    Example  

    To transfer control to the script section labelled as NXTREPORT when the error code generated by the last Teradata SQL request is 3000, enter the following command:

       .IF ERRORCODE = 3000 THEN .GOTO NXTREPORT

    Example  

    To set BTEQ’s return code to 1 if a Teradata SQL request finds no rows, enter the following command:

       .IF ACTIVITYCOUNT = 0 THEN .QUIT 1

    Example  

    When used in a Teradata SQL macro, the IF...THEN... command has the following format:

       ECHO '.IF ERRORCODE = 3000 THEN .REMARK ''Next'' ';