Teradata BTEQ Utility Guide
Teradata BTEQ Utility Guide
Table of Contents
Chapter 2: BTEQ..............................................................................................................................1
Why it is Called BTEQ?.........................................................................................................1
Logging onto BTEQ...............................................................................................................1
Using BTEQ to Submit Queries.............................................................................................2
Submitting SQL in BTEQ's Interactive Mode...................................................................2
WITH BY Statement...............................................................................................................3
Transactions in Teradata Mode.............................................................................................5
Alternative Transactions in Teradata Mode...........................................................................7
Transactions in ANSI Mode...................................................................................................8
Rollback.................................................................................................................................9
Advantages to ANSI Mode...................................................................................................10
Creating a Batch Script for BTEQ........................................................................................11
Running your Batch Script in BTEQ.....................................................................................12
Results from a BTEQ Batch Script.......................................................................................13
Placing our BTEQ Output to a file........................................................................................14
Reading out BTEQ output from the Text File.......................................................................14
Using BTEQ Conditional Logic............................................................................................15
Using BTEQ to Export Data.................................................................................................16
Executing our BTEQ Script to Export Data..........................................................................16
BTEQ Export Modes Explained...........................................................................................17
BTEQ EXPORT Example Using Record (DATA) Mode.......................................................18
BTEQ EXPORT Example Using Field (Report) Mode.........................................................19
BTEQ IMPORT Example.....................................................................................................21
Determining Out Record Lengths........................................................................................23
BTEQ Commands................................................................................................................25
Session Control Commands..........................................................................................25
File Control Commands.................................................................................................26
Sequence Control Commands.............................................................................................26
Format Control Commands............................................................................................26
Chapter 2: BTEQ
"Civilization advances by extending the number of important operations which we
can perform without thinking about them."
- Alfred Whitehead
There are other utilities that are faster than BTEQ for importing or exporting data. We will talk about
these in future chapters, but BTEQ is still used for smaller jobs.
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
Before you can use BTEQ, you must have user access rights to the client system and privileges to
the Teradata DBS. Normal system access privileges include a user ID and a password. Some
systems may also require additional user identification codes depending on company standards and
operational procedures. Depending on the configuration of your Teradata DBS, you may need to
include an account identifier (acctid) and/or a Teradata Director Program Identifier (TDPID).
First_Name
Squiggy
Herbert
John
Cletus
Salary Dept_No
32800.50
?
54500.00
400
48000.00
200
54500.00
400
Figure 2-1
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
BTEQ execution
Type at command
prompt: Logon
with TDPID and
USERNAME.
Then enter
PASSWORD at
the second
prompt.
BTEQ will
respond and is
waiting for a
command.
An SQL
Statement
.LOGON cdw/sql01
Password: XXXXX
First_name
Herbert
Cletus
Salary
54500.00
54500.00
BTEQ displays
information about
the answer set.
The result set
Dept_No
400
400
Figure 2-2
WITH BY Statement
"Time is the best teacher, but unfortunately, it kills all of its students."
Robin Williams
Investing time in Teradata can be a killer move for your career. We can use the WITH BY statement
in BTEQ, whereas we cannot use it with Nexus or SQL Assistant. The WITH BY statement works
like a correlated subquery in the fact that you can us aggregates based on a distinct column value.
BTEQ has the ability to use WITH BY statements:
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
"I've learned that you can't have everything and do everything at the same time."
Oprah Winfrey
The great thing about the WITH statement is that you can do everything to a specific group while
having everything done to a column as a whole. We can get a grand total or an overall average with
the WITH statement, just leave out BY. Here's a good example:
Using WITH on a whole column:
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
"What is defeat? Nothing but education; nothing but the first step to something
better."
Wendell Phillips
The final query in our last transaction is what caused our updates to fail. This was not the sweet
taste of victory, but instead the smell of de Feet! Actually, it really was an education leading to
something better. When using BT/ET in your transaction, you're telling Teradata that when it comes
to committing, we either want all or none. Since our last query in the transaction failed the Transient
Journal rolled back all the queries in our entire transaction. Make sure that your syntax is correct
when using the method of BT and ET because a mistake causes a massive rollback.
The last query in our set did not work:
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
Our updates didn't work! That's because we bundled all four queries into one transaction. Since our
last query failed, the tables were rolled back to their original state before the transaction took place.
Placing the semi-colon at the beginning of the next line (followed by another statement) will bundle
those statements together as one transaction. Notice that our Employee_Table was not updated,
Reprinted for [email protected], IBM
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
Notice that we have to COMMIT after any DDL or Update before the transaction is committed. We
even have to COMMIT after setting our DATABASE or we will get an error.
We didn't have any rows return, but we know there's a Mike Larkins within the table. That's because
BTEQ is case sensitive. Change 'larkins' to 'Larkins'.
Rollback
"Insanity: doing the same thing over and over again and expecting different results."
Albert Einstein
The Rollback keyword is the SQL mulligan of Teradata. Rollback will erase any changes made to a
table. This can be very useful if something didn't work. However, you cannot rollback once you've
used the commit keyword. Not keeping rollback in your arsenal would be insane.
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
10
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
11
"All truths are easy to understand once they are discovered; the point is to discover
them."
Galileo Galilei
Discovering the advantages in using ANSI will only make SQL easier to write. It might take a little bit
more typing, but a little work now can save you lots of time later.
The Employee_Table was updated!
In ANSI mode, only failed transactions are rolled back when it comes to multi-statement
transactions.
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
12
The following example shows how to create a batch script and how to invoke the script using BTEQ
from a DOS command. When using Batch scripting, your password will not be prompted. Instead,
just add the password after your login name, and a comma separates the two. Be sure to end with
either a .quit or .logoff so that your queries aren't left hanging.
Simply open up notepad and type in the following, then save it. I recommend calling it
'BTEQ_First_Batch_Script.txt' and save it in the C:\Temp folder. However, as long as you can
remember what you named it and where you saved it, you'll be fine. Be sure that you save it as a
.txt file.
Using Batch scripting with BTEQ
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
13
Once you're in DOS, type in the following: 'BTEQ < c:\temp\BTEQ_First_Script.txt', then hit enter.
BTEQ will automatically open in DOS, and then it will access the file from the location you listed.
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
14
Notice that the BTEQ command is immediately followed by the '<BTEQ_First_Script.txt' to tell BTEQ
which file contains the commands to execute. Then, the '>BTEQ_First_Export.txt' names the file
where the output messages are written.
Since putting password information into a script is scary for security reasons, inserting the password
directly into a script that is to be processed in batch mode may not be a good idea. It is generally
recommended and a common practice to store the logon and password in a separate file that that
can be secured. That way, it is not in the script for anyone to see. For example, the contents of a file
called "mylogon.txt" might be: '.LOGON cdw/sql00,whynot'. Then, the script should contain the
following command instead of a .LOGON: .RUN FILE=c:\temp\mylogon.txt. To submit results to
two different files at once, simply initialize BTEQ as follows:
BTEQ < c:\filename > c:\location1 > c:\location2
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
15
BTEQ conditional
logic that will
check to ensure
that the delete
worked or if the
table even
existed. If the
table did not
exist, then BTEQ
will create it. If
the table does
exist, the Create
table step will be
skipped and
directly GOTO
INSEMPS.
The Label
INSEMPS
provides code so
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
16
Last_name
CHAR(20),
First_name
CHAR(12),
Salary
DECIMAL(8,2),
Dept_No
SMALLINT)
UNIQUE PRIMARY INDEX (Employee_No);
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
17
these files is written in binary code, which is why the text seems garbled. It may look garbled, but it
is perfectly written. When we Fastload the data back to a table it will look beautiful.
Executing our fastload_creating_flatfile01.txt
And I thought French was tough; it's like they have a different word for everything
We now have a flat file that contains all information found in the Employee_Table. We will be able to
use this flat file for future exercises.
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
18
Therefore, the loading utility reads these bits as indicators of NULL data and identifies the column(s)
as NULL when data is loaded back into the table, where appropriate.
Since both DATA and INDICDATA store each column on disk in native format with known lengths
and characteristics, they are the fastest method of transferring data. However, it becomes
imperative that you be consistent. When it is exported as DATA, it must be imported as DATA and
the same is true for INDICDATA.
Again, this internal processing is automatic and potentially important. Yet, on a network-attached
system, being consistent is our only responsibility. However, on a mainframe system, you must
account for these bits when defining the LRECL in the Job Control Language (JCL). Otherwise, your
length is too short and the job will end with an error.
To determine the correct length, the following information is important. As mentioned earlier, one bit
is needed per field output onto disk. However, computers allocate data in bytes, not bits. Therefore,
if one bit is needed a minimum of eight (8 bits per byte) are allocated. Therefore, for every eight
fields, the LRECL becomes 1 byte longer and must be added. In other words, for nine columns
selected, 2 bytes are added even though only nine bits are needed.
With this being stated, there is one indicator bit per field selected. INDICDATA mode gives the Host
computer the ability to allocate bits in the form of a byte. Therefore, if one bit is required by the host
system, INDICDATA mode will automatically allocate eight of them. This means that from one to
eight columns being referenced in the SELECT will add one byte to the length of the record. When
selecting nine to sixteen columns, the output record will be two bytes longer.
When executing on non-mainframe systems, the record length is automatically maintained.
However, when exporting to a mainframe, the JCL (LRECL) must account for this additional 2 bytes
in the length.
DIF Mode: Known as Data Interchange Format, which allows users to export data from Teradata to
be directly utilized for spreadsheet applications like Excel, FoxPro and Lotus.
The optional LIMIT is to tell BTEQ to stop returning rows after a specific number (n) of rows. This
might be handy in a test environment to stop BTEQ before the end of transferring rows to the file.
LAN
syntax:
The following example uses a Record (DATA) Mode format. The output of the exported data will be
a flat file.
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
Employee_Table
Employee_No Last_Name
2000000
Jones
1256349
Harrison
1333454
Smith
1121334
Strickling
First_Name
Squiggy
Herbert
John
Cletus
Salary
32800.50
54500.00
48000.00
54500.00
19
Dept_No
?
400
200
400
Logon to TERADATA
.LOGON CDW/sql01,whynot;
.EXPORT DATA FILE = C:\EMPS.TXT
SELECT * FROM SQL_Class.Employee_Table;
.QUIT
Figure 2-6
DATABASE SQL_Class;
.EXPORT REPORT FILE = C:\EMPS.TXT
SELECT * FROM Employee_Table;
.EXPORT RESET
.LABEL Done
.QUIT
Figure 2-7
After this script has completed, the following report will be generated on disk.
Employee_No
2000000
1256349
1333454
1121334
1324657
2341218
1232578
Last_name
Jones
Harrison
Smith
Strickling
Coffing
Reilly
Chambers
First_name
Squiggy
Herbert
John
Cletus
Billy
William
Mandee
Salary
Dept_No
32800.50
?
54500.00
400
48000.00
200
54500.00
400
41888.88
200
36000.00
400
56177.50
100
Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
1000234
2312225
Smythe
Larkins
Richard
Loraine
64300.00
40200.00
20
10
300
I remember when my mom and dad purchased my first Lego set. I was so excited about building my
first space station that I ripped the box open, and proceeded to follow the instructions to complete
the station. However, when I was done, I was not satisfied with the design and decided to make
changes. So I built another space ship and constructed another launching station. BTEQ export
works in the same manner. As a user gains experience with BTEQ export, the easier it will get to
work with the utility.
With that being said, the following is an example that displays a more robust example of utilizing the
Field (Report) option. This example will export data in Field (Report) Mode format. The output of
the exported data will appear like a standard output of a SQL SELECT statement. In addition,
aliases and a title have been added to the script.
Logon to TERADATA
.LOGON CDW/sql01,whynot;
FROM
Last_name
AS "Last Name",
First_name AS "First Name",
Salary
AS "Salary",
Dept_No
AS "Department Number"
Employee_Table;
Figure 2-8
After then following script has been completed, the following report will be generated on disk.
Employee Number
Employee Profiles
Last Name First Name
2000000 Jones
1256349 Harrison
1333454 Smith
1121334 Strickling
1324657 Coffing
2341218 Reilly
1232578 Chambers
1000234 Smythe
2312225 Larkins
Squiggy
Herbert
John
Cletus
Billy
William
Mandee
Richard
Loraine
Salary
32800.50
54500.00
48000.00
54500.00
41888.88
36000.00
56177.50
64300.00
40200.00
Department
Number
?
400
200
400
200
400
100
10
300
From the above example, a number of BTEQ commands were added to the export script. Below is a
review of those commands.
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
21
The WIDTH specifies the width of screen displays and printed reports, based on characters
per line.
The FORMAT command allows the ability to enable/inhibit the page-oriented format option.
The HEADING command specifies a header that will appear at the top every page of a
report.
The script below introduces the IMPORT command with the Record (DATA) option. Notice the
periods (.) at the beginning some of script lines. A period starting a line indicates a BTEQ command.
If there is no period, then the command is an SQL command.
The SKIP option is used when you wish to bypass the first records in a file. For example, a
mainframe tape may have header records that should not be processed. Other times, maybe the job
started and loaded a few rows into the table with a UPI defined. Loading them again will cause an
error. So, you can skip over them using this option.
The following example will use a Record (DATA) Mode format. The input of the imported data will
populate the Employee_Table.
Specify the number of SESSIONS to establish
with Teradata
.SESSIONS 4
.LOGON CDW/sql01,whynot;
Logon to TERADATA
.IMPORT DATA FILE = C:\EMPS.TXT, SKIP = 2
.QUIET ON
.REPEAT *
USING
Emp_No
L_name
F_name
Salary
Dept_No
(INTEGER),
(CHAR(20)),
(VARCHAR(12)),
(DECIMAL(8,2)),
(SMALLINT)
(:Emp_No,
:L_name,
:F_name,
:Salary,
:Dept_No) ;
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
22
.QUIT
Figure 2-9
From the above example, a number of BTEQ commands were added to the import script. The next
page contains a review of those commands.
.QUIET ON limits BTEQ output to reporting only errors and request processing statistics.
Note: Be careful how you spell .QUIET, else forgetting the E becomes .QUIT and it will.
.REPEAT * causes BTEQ to read a specified number of records or until EOF. The default is
one record. Using REPEAT 10 would perform the loop 10 times.
The USING defines the input data fields and their associated data types coming from the
host.
The following builds upon the IMPORT Record (DATA) example above. The example below will still
utilize the Record (DATA) Mode format. However, this script adds a CREATE TABLE statement. In
addition, the imported data will populate the newly created Employee_Profile table.
Specify the number of SESSIONS to establish
with Teradata
Logon to TERADATA
.SESSIONS 2
.LOGON CDW/sql01,whynot;
.REPEAT 120
USING
Employee_No
Last_name
First_name
Salary
Dept_No
(INTEGER),
(CHAR(20)),
(VARCHAR(12)),
(DECIMAL(8,2)),
(SMALLINT)
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
VALUES
(:Employee_No,
:Last_name,
:First_name,
:Salary,
:Dept_No) ;
23
.LOGOFF
.QUIT
Figure 2-10
Notice that some of the scripts have a .LOGOFF and .QUIT. The .LOGOFF is optional because
when BTEQ quits, the session is terminated. A logoff makes it a friendly departure and also allows
you to logon with a different user name and password.
Fixed Length Columns: For fixed length columns you merely count the length of the column. The
lengths are:
4 bytes
INTEGER
2 bytes
SMALLINT
1 byte
BYTEINT
10 bytes
CHAR(10)
4 bytes
CHAR(4)
4 bytes
DATE
Reprinted for [email protected], IBM
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
24
Variable columns: Variable length columns should be calculated as the maximum value plus two.
The two bytes are for the number of bytes for the binary length of the field. In reality you can save
much space because trailing blanks are not kept. The logical record will assume the maximum and
add two bytes as a length field per column.
10 bytes
VARCHAR(8)
12 bytes
VARCHAR(10)
Indicator columns: As explained earlier, the indicators utilize a single bit for each field. If your record
has 8 fields (which require 8 bits), then you add one extra byte to the total length of all the fields. If
your record has 9-16 fields, then add two bytes.
BTEQ Return Codes
Return codes are two-digit values that BTEQ returns to the user after completing each job or task.
The value of the return code indicates the completion status of the job or task as follows:
Return Code Descirption
00 Job completed with no errors.
02 User alert to log on to the Teradata DBS.
04 Warning error.
08 User error.
12 Severe internal error.
You can over-ride the standard error codes at the time you terminate BTEQ. This might be handy
for debugging purposes. The error code or "return code" can be any number you specify using one
of the following:
Override Code Description
.QUIT 15
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
25
.EXIT 15
BTEQ Commands
The BTEQ commands in Teradata are designed for flexibility. These commands are not used
directly on the data inside the tables. However, these 60 different BTEQ commands are utilized in
four areas.
Session Control Commands
File Control Commands
Sequence Control Commands
Format Control Commands
Abort any and all active running requests and transactions for a session, but
do not exit BTEQ.
DEFAULTS
Reset all BTEQ Format command options to their defaults. This will utilize
the default configurations.
EXIT
Immediately end the current session or sessions and exit BTEQ.
HALT EXECUTION Abort any and all active running requests and transactions and EXIT BTEQ.
LOGOFF
End the current session or sessions, but do not exit BTEQ.
LOGON
Starts a BTEQ Session. Every user, application, or utility must LOGON to
Teradata to establish a session.
QUIT
End the current session or sessions and exit BTEQ.
SECURITY
Specifies the security level of messages between a network-attached
system and the Teradata Database.
SESSIONS
Specifies the number of sessions to use with the next LOGON command.
SESSION
Specifies the name of a character set for the current session or sessions.
CHARSET
SESSION
Specifies a disposition of warnings issued in response to violations of ANSI
SQLFLAG
syntax. The SQL will still run, but a warning message will be provided. The
four settings are FULL, INTERMEDIATE, ENTRY, and NONE.
SESSION
Specifies whether transaction boundaries are determined by Teradata SQL
TRANSACTION
or ANSI SQL semantics.
SHOW
Displays all of the BTEQ control command options currently configured.
CONTROLS
SHOW VERSIONS Displays the BTEQ software release versions.
TDP
Used to specify the correct Teradata server for logons for a particular session.
Figure 2-11
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
26
HALT
EXECUTION
FORMAT
IMPORT
INDICDATA
OS
QUIET
RECORDMODE
REPEAT
RUN
TSO
Figure 2-12
Figure 2-13
Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition
27
DEFAULTS
Reset all BTEQ Format command options to their defaults. This will utilize the
default configurations.
ECHOREQ
Enable the Echo required function in BTEQ returning a copy of each
Teradata SQL request and BTEQ command to the standard output stream.
EXPORT
Open a file with a specific format to transfer information directly from the
Teradata database.
FOLDLINE
Split or fold each line of a report into multiple lines.
FOOTING
Specify a footer to appear at the bottom of every report page.
FORMAT
Enable/inhibit the page-oriented format command options.
IMPORT
Open a file with a specific format to transfer or IMPORT information directly
to Teradata.
NULL
Specifies a character or string of characters to represent null values returned
from Teradata.
OMIT
Omit specific columns from a report.
PAGEBREAK
Ejects a page whenever a specified column changes values.
PAGELENGTH
Specifies the page length of printed reports based on lines per page.
QUIET
Limit BTEQ output displays to all error messages and request processing
statistics.
RECORDMODE
One of multiple data mode options for data selected from Teradata.
(INDICDATA, FIELD, or RECORD).
RETCANCEL
Cancel a request when the specified value of the RETLIMIT command option
is exceeded.
RETLIMIT
Specifies the maximum number of rows to be displayed or written from a
Teradata SQL request.
RETRY
Retry requests that fail under specific error conditions.
RTITLE
Specify a header appearing at the top of all pages of a report.
SEPARATOR
Specifies a character string or specific width of blank characters separating
columns of a report.
SHOWCONTROLS Displays all of the BTEQ control command options currently configured.
SIDETITLES
Place titles to the left or side of the report instead of on top.
SKIPLINE
Inserts blank lines in a report when the value of a column changes specified
values.
SUPPRESS
Replace each and every consecutively repeated value with completely-blank
character strings.
TITLEDASHES
Display dash characters before each report line summarized by a WITH
clause.
UNDERLINE
Display a row of dash characters when the specified column changes
values.
WIDTH
Specifies the width of screen displays and printed reports, based on characters
per line.
Figure 2-14