Chapter 2 BTEQ

24
 Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition by Tom Coffing et al. Coffing Data Warehousing. (c) 2005. Copying Prohibited. Reprinted for Raghavendra K M, Hewlett Packard [email protected] Reprinted with permission as a subscription benefit of Skillport, http://skillport.books24x7.com/  All rights reserved. Reproduction and/or distribution in whole or in part in electronic,pape r or other forms without written permission is prohibited.  

Transcript of Chapter 2 BTEQ

Page 1: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 1/24

 

Teradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, andFastExport, Second Edition 

by Tom Coffing et al.Coffing Data Warehousing. (c) 2005. Copying Prohibited.

Reprinted for Raghavendra K M, Hewlett Packard 

[email protected] 

Reprinted with permission as a subscription benefit of Skillport,

http://skillport.books24x7.com/ 

All rights reserved. Reproduction and/or distribution in whole or in part in electronic,paper orother forms without written permission is prohibited. 

Page 2: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 2/24

Chapter 2: BTEQ

"Civilization advances by extending the number of  important operations which we can perform without  thinking

about them."  

- Alfred Whitehead 

Why it is Called BTEQ?

Why is BTEQ available on every Teradata system ever built? Because the Batch TEradata Query (BTEQ) tool was theoriginal way that SQL was submitted to Teradata as a means of getting an answer set in a desired format. This is the utilitythat I used for training at Wal-Mart, AT&T, Anthem Blue Cross and Blue Shield, and SouthWestern Bell back in the early1990's. BTEQ is often referred to as the Basic TEradata Query and is still used today as an effective tool.

Here is what is excellent about BTEQ:

n BTEQ can be used to submit SQL in either a batch or interactive environment. Interactive users can submit SQL andreceive an answer set on the screen. Users can also submit BTEQ jobs from batch scripts, have error checking andconditional logic, and allow for the work to be done in the background.

n BTEQ outputs a report format, where Queryman outputs data in a format more like a spreadsheet. This allows BTEQ agreat deal of flexibility in formatting data, creating headings, and utilizing Teradata extensions, such as WITH andWITH BY that Queryman has problems in handling.

n BTEQ is often used to submit SQL, but is also an excellent tool for importing and exporting data.

¡ Importing Data: Data can be read from a file on either a mainframe or LAN attached computer and used forsubstitution directly into any Teradata SQL using the INSERT, UPDATE or DELETE statements.

¡ Exporting Data: Data can be written to either a mainframe or LAN attached computer using a SELECT fromTeradata. You can also pick the format you desire ranging from data files to printed reports to spread sheetformats.

There are other utilities that are faster than BTEQ for importing or exporting data. We will talk about these in futurechapters, but BTEQ is still used for smaller jobs.

Logging onto BTEQ

"It's choice  – not change  – that determines your destiny."  

 – Jean Nidetch 

By taking a chance in this industry, you've chosen to arm yourself with an unlimited arsenal of knowledge. But you can'tuse that knowledge if you can't log onto the system! This next slide is going to teach you how to logon to BTEQ.Remember that you will be prompted for the password since it's an interactive interface. BTEQ commands begin with aperiod (.) and do not require a semi-colon (;) to end the statement. SQL commands do not ever start with a period and theymust always be terminated with a semi-colon.

Let's logon to BTEQ and show all information in the Employee_Table:

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 2 / 24

Page 3: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 3/24

 

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 useridentification codes depending on company standards and operational procedures. Depending on the configuration of yourTeradata DBS, you may need to include an account identifier (acctid) and/or a Teradata Director Program Identifier(TDPID).

Using BTEQ to Submit Queries

Submitting SQL in BTEQ's Interactive Mode

Once you logon to Teradata through BTEQ, you are ready to run your queries. Teradata knows the SQL is finished whenit finds a semi-colon, so don't forget to put one at the end of your query. Below is an example of a Teradata table todemonstrate BTEQ operations.

Employee_Table 

Figure 2-1  

BTEQ execution 

Employee_No Last_Name First_Name Salary Dept_No

2000000  Jones  Squiggy  32800.50  ? 

1256349  Harrison  Herbert  54500.00  400 

1333454  Smith  John  48000.00  200 

1121334  Strickling  Cletus  54500.00  400 

.LOGON cdw/sql01 

Password: XXXXX 

Type at command prompt: Logon with TDPID and

USERNAME. Then enter PASSWORD at the second

prompt. 

Enter your BTEQ/SQL Request or BTEQ Command.  BTEQ will respond and is waiting for a command. 

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 3 / 24

Page 4: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 4/24

 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 wecannot use it with Nexus or SQL Assistant. The WITH BY statement works like a correlated subquery in the fact that youcan us aggregates based on a distinct column value.

BTEQ has the ability to use WITH BY statements:

"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

SELECT * FROM Employee_Table 

WHERE Dept_No = 400; 

An SQL Statement 

*** Query Completed. 2 rows found. 5 Columns returned.

*** Total elapsed time was 1 second. 

BTEQ displays information about the answer set. 

Employee_No Last_name First_name Salary Dept_No 

1256349 Harrison Herbert 54500.00 400 

1121334 Strickling Cletus 54500.00 400 

The result set 

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 4 / 24

Page 5: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 5/24

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'sa good example:

Using WITH on a whole column:

Transactions in Teradata Mode

"He who every morning plans the transaction of the day and follows out that plan, carries a thread that will  

guide him through the maze of most busy life."  

- Victor Hugo 

Victor couldn't have summed up Teradata any better. However, Victor did seem more worried about the hunchback thanthe rollback. Turning your queries into a single transaction is often the best plan, but can sometimes make one Miserables.

Often in Teradata we'll see multiple queries within the same transaction. We can use the BT/ET keywords to bundleseveral queries into one transaction. You also need to end every query with a semi-colon (;), which isn't the case in Nexusor SQL assistant. For example:

In Teradata mode, we're going to put four single statements into a single transaction

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 5 / 24

Page 6: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 6/24

 

"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, butinstead the smell of de Feet! Actually, it really was an education leading to something better. When using BT/ET in yourtransaction, you're telling Teradata that when it comes to committing, we either want all or none. Since our last query in thetransaction failed the Transient Journal rolled back all the queries in our entire transaction. Make sure that your syntax iscorrect when using the method of BT and ET because a mistake causes a massive rollback.

The last query in our set did not work:

Now let's take a look at the Employee_Table:

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 6 / 24

Page 7: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 7/24

Page 8: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 8/24

Let's log back onto BTEQ, but this time change it to ANSI mode:

"Be not afraid of growing slowly, be afraid  only of standing still."  

-Chinese Proverb 

Remember the first rule of ANSI mode: all transactions must be committed by the user actually using the word 'COMMIT'. Also, in ANSI mode after any DDL statement (CREATE, DROP, ALTER, DATABASE) we have to use the 'commit'command immediately. This tells Teradata to commit to what's been done. Our query below will attempt to find anyone witha last_name of 'larkins'. It will fail even though we have 'Mike' 'Larkins' in our table. This is because ANSI is case sensitiveand we did not capitalize the 'L' in 'Larkins'.

Let's run a few queries in ANSI mode:

Notice that we have to COMMIT after any DDL or Update before the transaction is committed. We even have toCOMMIT 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 casesensitive. Change 'larkins' to 'Larkins'.

Rollback

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 8 / 24

Page 9: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 9/24

"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 veryuseful if something didn't work. However, you cannot rollback once you've used the commit keyword. Not keeping rollbackin your arsenal would be insane.

Advantages to ANSI Mode

 ANSI mode is great because when you bundle several queries into one transaction and one doesn't work, the rest won't berolled back to their original state. Using commit will ensure that your successes aren't hidden by your failures.

Now notice that I will have multiple statements in the same transaction and that I purposely fail the last SQL statement:

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 9 / 24

Page 10: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 10/24

 

Which statements were rolled back?

"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 littlework now can save you lots of time later.

The Employee_Table was updated!

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 10 / 24

Page 11: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 11/24

In ANSI mode, only failed transactions are rolled back when it comes to multi-statement transactions.

Creating a Batch Script for BTEQ

"The cure for boredom is curiosity. There is no cure for curiosity."  

 – Dorothy Parker  

If you've been bored waiting for your queries to finish, then I'm sure you're curious as to how we can fix the situation. Batchscripting allows us to write out pages and pages of queries and execute those queries in one single swoop. BTEQ can alsorun in batch mode under UNIX (IBM AIX , Hewlett-Packard HP-UX , NCR MP-RAS, Sun Solaris), DOS, Macintosh,

Microsoft Windows and OS/2 operating systems. To submit a job in batch mode, do the following:

1. Invoke BTEQ (using dos prompt)

2. Type in the input file name

3. Type in the location and output file name.

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' andsave it in the C:\Temp folder. However, as long as you can remember what you named it and where you saved it, you'll befine. Be sure that you save it as a .txt file.

Using Batch scripting with BTEQ

Running your Batch Script in BTEQ

"I do not fear computers. I fear the lack of  them."  

 – Isaac Asimov 

The BTEQ utility enables us to run our scripts in batch mode. To run our new batch script, we have to access the BTEQutility via dos prompt. Simply use command prompt to access the utility, and follow the steps below:

Let's run our query in Batch!

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 11 / 24

Page 12: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 12/24

 

Once you're in DOS, type in the following: 'BTEQ < c:\temp\BTEQ_First_Script.txt', then hit enter. BTEQ will automaticallyopen in DOS, and then it will access the file from the location you listed.

Results from a BTEQ Batch Script

"Don't be afraid to take a big step when one is indicated. You can't cross a chasm in two small steps."  

-David Lloyd George 

BTEQ will run your query in steps to produce the answer you're looking for. Whether you're accessing a small table orcrossing over a chasm of information, BTEQ will ensure that the steps it takes will be big enough to get the job done.

Our results are returned Interactively

Placing our BTEQ Output to a file

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 12 / 24

Page 13: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 13/24

"The secret to creativity is knowing how to hide your sources."  

 – Albert Einstein 

We can use BTEQ to export our results to another text document. Exporting data also works very well when you're trying todocument your query along with the results.

We can export our results in batch as well

Notice that the BTEQ command is immediately followed by the '<BTEQ_First_Script.txt' to tell BTEQ which file contains thecommands 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 scriptthat is to be processed in batch mode may not be a good idea. It is generally recommended and a common practice tostore 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 shouldcontain the following command instead of a .LOGON: .RUN FILE=c:\temp\mylogon.txt. To submit results to two differentfiles at once, simply initialize BTEQ as follows:

BTEQ < c:\filename > c:\location1 > c:\location2 

Reading out BTEQ output from the Text File

"The more original a discovery, the more obvious it seems afterwards."  

 – Arthur Koestler  

Discovering how easy it is to export your data in batch mode is a key step in learning Teradata utilities. Here are ourresults, including the original query and what BTEQ did to generate its answer set. Simply go to the folder where yousaved the exported data (the previous examples saved the file as c:\temp\BTEQ_First_Export.txt).

What you'll find in our new text document

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 13 / 24

Page 14: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 14/24

 

Using BTEQ Conditional Logic

"I was walking down the street wearing glasses when the prescription ran out."  

- Steven Wright 

Below is a BTEQ batch script example. The initial steps of the script will establish the logon, the database, and then deleteall the rows from the Employee_Table. If the table does not exist, the BTEQ conditional logic will instruct Teradata tocreate it. However, if the table already exists, then Teradata will move forward and insert data.

Note: In script examples, the left panel contains BTEQ base commands and the right panel provides a brief description ofeach command.

.RUN FILE = c:\temp\mylogon.txt 

DATABASE SQL_Class; 

DELETE FROM Employee_Table; 

.IF ERRORCODE = 0 THEN .GOTO INSEMPS[*] 

/* ERRORCODE is a reserved word that contains the outcome  

status for every SQL statement executed in BTEQ. A zero (0)  

indicates that statement worked. */ 

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. 

CREATE TABLE Employee_Table (Employee_No INTEGER, Last_name CHAR(20), First_name CHAR(12), Salary DECIMAL(8,2), Dept_No SMALLINT) 

UNIQUE PRIMARY INDEX (Employee_No); 

The Label INSEMPS provides code so the BTEQLogic can go directly to inserting records into the

Employee_Table. 

.LABEL INSEMPS[*] 

INSERT INTO Employee_Table (1232578, 'Chambers', 'Mandee', 48850.00, 

100); 

INSERT INTO Employee_Table (1256349, 'Harrison', 'Herbert', 54500.00, 400);

.QUIT 

Once the table has been created, Teradata will

then insert the two new rows into the empty table. 

[*]Both labels have to be identical or it will not work. 

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 14 / 24

Page 15: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 15/24

Using BTEQ to Export Data

"The trouble with facts is that there are so  many of them."  

 – Samuel McChord Crothers 

Creating flat files is one of the most important tasks in Teradata, and that's a fact. BTEQ allows data to be exported directlyfrom Teradata to a file on a mainframe or network-attached computer. In addition, the BTEQ export function has several

export formats that a user can choose from depending on the desired output. Generally, users will export data to a flat fileformat that is composed of a variety of characteristics. These characteristics include: field mode, indicator mode, or difmode.

Syntax of a basic EXPORT command:

EXPORT <Mode (example: data)> FILE = <filename> 

Creating a flat file of what's on the Employee_Table

Executing our BTEQ Script to Export Data

"The past is a foreign country; they do things differently there."  

L. P. Hartley

Transferring data from table to another without the use of a flat file is a thing of the past. Teradata does things differentlynow, which is why it's still considered the future of data warehousing. The flat files we create are merely used to storeinformation contained within a table. The information on these files is written in binary code, which is why the text seemsgarbled. 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

What our flat file looks like:

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 15 / 24

Page 16: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 16/24

 

 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 forfuture exercises.

BTEQ Export Modes Explained

Below is a list and description of our three data modes:

Record Mode: (also called DATA mode): This is set by .EXPORT DATA. This will bring data back as a flat file. Eachparcel will contain a complete record. Since it is not a report, there are no headers or white space between the datacontained in each column and the data is written to the file (e.g., disk drive file) in native format. For example, this meansthat INTEGER data is written as a 4-byte binary field. Therefore, it cannot be read and understood using a normal texteditor.

Field Mode (also called REPORT mode): This is set by .EXPORT REPORT. This is the default mode for BTEQ andbrings the data back as if it was a standard SQL SELECT statement. The output of this BTEQ export would return thecolumn headers for the fields, white space, expanded packed or binary data (for humans to read) and can be understoodusing a text editor.

Indicator Mode: This is set by .EXPORT INDICDATA. This mode writes the data in data mode, but also provides host

operating systems with the means of recognizing missing or unknown data (NULL) fields. This is important if the data is tobe loaded into another Relational Database System (RDBMS).

The issue is that there is no standard character defined to represent either a numeric or character NULL. So, every systemuses a zero for a numeric NULL and a space or blank for a character NULL. If this data is simply loaded into anotherRDBMS, it is no longer a NULL, but a zero or space.

To remedy this situation, INDICATA puts a bitmap at the front of every record written to the disk. This bitmap contains onebit per field/column. When a Teradata column contains a NULL, the bit for that field is turned on by setting it to a "1".Likewise, if the data is not NULL, the bit remains a zero. Therefore, the loading utility reads these bits as indicators ofNULL 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 asDATA, 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 consistentis our only responsibility. However, on a mainframe system, you must account for these bits when defining the LRECL inthe 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 fieldoutput onto disk. However, computers allocate data in bytes, not bits. Therefore, if one bit is needed a minimum of eight (8bits per byte) are allocated. Therefore, for every eight fields, the LRECL becomes 1 byte longer and must be added. Inother 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 toallocate bits in the form of a byte. Therefore, if one bit is required by the host system, INDICDATA mode will automaticallyallocate eight of them. This means that from one to eight columns being referenced in the SELECT will add one byte to thelength of the record. When selecting nine to sixteen columns, the output record will be two bytes longer.

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 16 / 24

Page 17: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 17/24

When executing on non-mainframe systems, the record length is automatically maintained. However, when exporting to amainframe, 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 forspreadsheet 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.

BTEQ EXPORT Example Using Record (DATA) Mode

The following is an example that displays how to utilize the export Record (DATA) option. Notice the periods (.) at thebeginning some of script lines. A period starting a line indicates a BTEQ command. If there is no period, then the commandis an SQL command.

When doing an export on a mainframe or a network-attached (e.g., LAN) computer, there is one primary difference inthe .EXPORT command. The difference is the following:

The following example uses a Record (DATA) Mode format. The output of the exported data will be a flat file.

Employee_Table

Figure 2-6  

BTEQ EXPORT Example Using Field (Report) Mode

The following is an example that displays how to utilize the export Field (Report) option. Notice the periods (.) at thebeginning some of script lines. A period starting a line indicates a BTEQ command and needs no semi-colon. Likewise, ifthere is no period, then the command is an SQL command and requires a semi-colon.

n Mainframe syntax:.EXPORT DATA DDNAME = data definition statement name (JCL)

n LAN syntax: .EXPORT DATA FILE = actual file name 

Employee_No Last_Name First_Name Salary Dept_No

2000000 Jones Squiggy 32800.50 ?

1256349 Harrison Herbert 54500.00 400

1333454 Smith John 48000.00 200

1121334 Strickling Cletus 54500.00 400

.LOGON CDW/sql01,whynot;  Logon to TERADATA 

.EXPORT DATA FILE = C:\EMPS.TXT  This Export statement will be in record (DATA)

mode. 

SELECT * FROM SQL_Class.Employee_Table;  The EMPS.TXT file will be created as a flat file 

.QUIT  Finish the execution. 

.LOGON CDW/sql01,whynot;  Logon to TERADATA 

DATABASE SQL_Class; 

.EXPORT REPORT FILE = C:\EMPS.TXT

 

This Export statement will be in field (REPORT) mode. The EMPS.TXT file will becreated as a report. 

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 17 / 24

Page 18: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 18/24

 Figure 2-7  

 After this script has completed, the following report will be generated on disk.

I remember when my mom and dad purchased my first Lego set. I was so excited about building my first space station that Iripped 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 launchingstation. BTEQ export works in the same manner. As a user gains experience with BTEQ export, the easier it will get towork 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 standardoutput of a SQL SELECT statement. In addition, aliases and a title have been added to the script.

SELECT * FROM Employee_Table; 

.IF ERRORCODE > 0 THEN .GOTO Done

 

SELECT * FROM Department_Table; 

BTEQ checks to ensure no errors occurred and selects more rows  – else GOTO

Done. 

.EXPORT RESET 

.LABEL Done 

.QUIT 

Reverse previous export command and fall through to Done. 

Employee_No Last_name First_name Salary Dept_No

2000000 Jones Squiggy 32800.50 ?

1256349 Harrison Herbert 54500.00 4001333454 Smith John 48000.00 200

1121334 Strickling Cletus 54500.00 400

1324657 Coffing Billy 41888.88 200

2341218 Reilly William 36000.00 400

1232578 Chambers Mandee 56177.50 100

1000234 Smythe Richard 64300.00 10

2312225 Larkins Loraine 40200.00 300

.LOGON CDW/sql01,whynot;  Logon to TERADATA 

.SET WIDTH 90 

.SET FORMAT ON 

.SET HEADING 'Employee Profiles' 

Set the format parameters for the final report 

.EXPORT REPORT FILE = C:\EMP_REPORT.TXT This Export statement will be in field (REPORT) mode. The

EMP_REPORT.TXT file will be created as a report. 

SELECT Employee_No AS "Employee Number", 

Last_name AS "Last Name", 

First_name AS "First Name", 

Salary AS "Salary", 

Dept_No AS "Department Number" 

FROM Employee_Table; 

Specifies the columns that are being selected. Notice that the columns

have an alias. 

.EXPORT RESET 

.QUIT 

Reverse previous export command effects 

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 18 / 24

Page 19: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 19/24

Figure 2-8  

 After then following script has been completed, the following report will be generated on disk.

From the above example, a number of BTEQ commands were added to the export script. Below is a review of those

commands.

n The WIDTH specifies the width of screen displays and printed reports, based on characters per line.

n The FORMAT command allows the ability to enable/inhibit the page-oriented format option.

n The HEADING command specifies a header that will appear at the top every page of a report.

BTEQ IMPORT Example

BTEQ can also read a file from the hard disk and incorporate the data into SQL to modify the contents of one or moretables. In order to do this processing, the name and record description of the file must be known ahead of time. These willbe defined within the script file.

Format of the IMPORT command:

.IMPORT <mode> { FILE | DNAME } = <filename> [,SKIP=n] 

The script below introduces the IMPORT command with the Record (DATA) option. Notice the periods (.) at the beginningsome of script lines. A period starting a line indicates a BTEQ command. If there is no period, then the command is an SQLcommand.

The SKIP option is used when you wish to bypass the first records in a file. For example, a mainframe tape may haveheader records that should not be processed. Other times, maybe the job started and loaded a few rows into the table witha 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 theEmployee_Table.

Employee Profiles

Employee Number Last Name First Name Salary Department Number

2000000 Jones Squiggy 32800.50 ?1256349 Harrison Herbert 54500.00 400

1333454 Smith John 48000.00 200

1121334 Strickling Cletus 54500.00 400

1324657 Coffing Billy 41888.88 200

2341218 Reilly William 36000.00 400

1232578 Chambers Mandee 56177.50 100

1000234 Smythe Richard 64300.00 10

2312225 Larkins Loraine 40200.00 300

.SESSIONS 4 

.LOGON CDW/sql01,whynot; 

Specify the number of SESSIONS to establish with Teradata  

.IMPORT DATA FILE = C:\EMPS.TXT, SKIP = 2 Logon to TERADATA 

.QUIET ON 

.REPEAT * 

USING Emp_No (INTEGER), 

L_name (CHAR(20)), 

F_name (VARCHAR(12)), Salary (DECIMAL(8,2)), 

Dept_No (SMALLINT) 

Specify DATA mode, name the file to read "EMPS.TXT", but skip the first 2

records. 

Limit messages out. Loop in this script until end of records in file. 

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 19 / 24

Page 20: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 20/24

 Figure 2-9  

From the above example, a number of BTEQ commands were added to the import script. The next page contains a reviewof those commands.

n .QUIET ON limits BTEQ output to reporting only errors and request processing statistics. Note: Be careful how youspell .QUIET, else forgetting the E becomes .QUIT and it will.

n .REPEAT * causes BTEQ to read a specified number of records or until EOF. The default is one record. UsingREPEAT 10 would perform the loop 10 times.

n 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 populatethe newly created Employee_Profile table.

INSERT INTO Employee_Table 

(Employee_No, Last_name, First_name, Salary, Dept_No) 

The USING Specifies the field in the input file and names them. 

VALUES (:Emp_No, :L_name, :F_name, :Salary, :Dept_No) ; 

Specify the insert parameters for the employee_table 

Substitutes data from the fields into the SQL command. 

.QUIT  Exit the script after all data read and rows inserted. 

.SESSIONS 2  Specify the number of SESSIONS to establish with Teradata  

.LOGON CDW/sql01,whynot;  Logon to TERADATA 

DATABASE SQL_Class;  Make the default database SQL_Class 

CREATE TABLE Employee_Profile 

Employee_No INTEGER, 

Last_name CHAR(20), 

First_name VARCHAR(12), Salary DECIMAL(8,2), Dept_No SMALLINT 

) UNIQUE PRIMARY INDEX (Employee_No) ; .IMPORT INDICDATA FILE = C:\IND-EMPS.TXT

This statement will create the Employee_Profile table. 

.QUIET ON  This import statement specifies INDICDATA mode. The input file is from a

LAN file called IND-EMPS.TXT. 

.REPEAT 120  Quiet on limits the output to reporting only errors and processing statistics. 

USING Employee_No (INTEGER), 

Last_name (CHAR(20)), 

First_name (VARCHAR(12)), Salary (DECIMAL(8,2)), 

Dept_No (SMALLINT) 

This causes BTEQ to read the first 120 records from the file. 

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 20 / 24

Page 21: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 21/24

 Figure 2-10  

Notice that some of the scripts have a .LOGOFF and .QUIT. The .LOGOFF is optional because when BTEQ quits, thesession is terminated. A logoff makes it a friendly departure and also allows you to logon with a different user name andpassword.

Determining Out Record Lengths

Some hosts, such as IBM mainframes, require the correct LRECL (Logical Record Length) parameter in the JCL, and willabort if the value is incorrect. The following page will discuss how to figure out the record lengths.

There are three issues involving record lengths and they are:

n Fixed columns

n Variable columns

n NULL indicators

Fixed Length Columns: For fixed length columns you merely count the length of the column. The lengths are:

Variable columns: Variable length columns should be calculated as the maximum value plus two. The two bytes are for thenumber 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.

INSERT INTO Employee_Profile 

(Employee_No, Last_name, First_name, Salary, Dept_No) 

The USING Specifies the parameters of the input file. 

VALUES (:Employee_No, :Last_name, :First_name, :Salary, :Dept_No) ; 

Specify the insert parameters for the employee_profile. 

.LOGOFF 

.QUIT 

Substitute the values to be inputted into the SQL command. 

n INTEGER4 bytes

n SMALLINT2 bytes

n BYTEINT1 byte

n CHAR(10)10 bytes

n CHAR(4)4 bytes

n DATE4 bytes

n DECIMAL(7,2)4 bytes (packed data, total digits / 2 +1)

n DECIMAL(12,2)7 bytes

n VARCHAR(8)10 bytes

n VARCHAR(10) 12 bytes

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 21 / 24

Page 22: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 22/24

Indicator columns: As explained earlier, the indicators utilize a single bit for each field. If your record has 8 fields (whichrequire 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 twobytes.

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 

n 00 Job completed with no errors.

n 02 User alert to log on to the Teradata DBS.

n 04 Warning error.

n 08 User error.

n 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 

n .QUIT 15

n .EXIT 15

BTEQ Commands

The BTEQ commands in Teradata are designed for flexibility. These commands are not used directly on the data insidethe tables. However, these 60 different BTEQ commands are utilized in four areas.

n Session Control Commands

n File Control Commands

n Sequence Control Commands

n Format Control Commands

Session Control Commands

 ABORT 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.

SESSIONCHARSET

Specifies the name of a character set for the current session or sessions.

SESSIONSQLFLAG

Specifies a disposition of warnings issued in response to violations of ANSI syntax. The SQL will still run,but a warning message will be provided. The four settings are FULL, INTERMEDIATE, ENTRY, and NONE. 

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 22 / 24

Page 23: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 23/24

 

Figure 2-11  

File Control Commands

These BTEQ commands are used to specify the formatting parameters of incoming and outgoing information. This includesidentifying sources and determining I/O streams.

Figure 2-12  

Sequence Control Commands

These commands control the sequence in which Teradata commands operate.

Figure 2-13  

Format Control Commands

SESSIONTRANSACTION

Specifies whether transaction boundaries are determined by Teradata SQL or ANSI SQL semantics.

SHOW CONTROLS Displays all of the BTEQ control command options currently configured.

SHOW VERSIONS Displays the BTEQ software release versions.

TDP Used to specify the correct Teradata server  for logons for a particular session.

CMS Execute a VM CMS command inside the BTEQ environment.

ERROROUT Write error  messages to a specific output file.

EXPORT Open a file with a specific format to transfer information directly from the Teradata database.

HALT

EXECUTION

Abort any and all active running requests and transactions and EXIT BTEQ.

FORMAT Enable/inhibit the page-oriented format command options.

IMPORT Open a file with a specific format to import information into Teradata.

INDICDATA One of multiple data mode options for data selected from Teradata. The modes are INDICDATA, FIELD, or

RECORD MODE. 

OS Execute an MS-DOS, PC-DOS, or UNIX command from inside BTEQ.

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). 

REPEAT Submit the next request a certain amount of times

RUN Execute Teradata SQL requests and BTEQ commands directly from a specified run file.

TSO Execute an MVS TSO command from inside the BTEQ environment.

 ABORT Abort any active transactions and requests.

ERRORLEVEL Assign severity levels to particular error numbers.

EXIT End the current session or sessions and exit BTEQ.

GOTO Skip all intervening commands and resume after branching forward to the specified label.

HANG Pause BTEQ processing for a specific amount of time.

IF…THEN Test a stated condition, and then resume processing based on the test results.

LABEL The GOTO command will always GO directly TO a particular line of code based on a label.

MAXERROR Specifies a maximum allowable error severity level.

QUIT End the current session or sessions and exit BTEQ.

REMARK Place a comment on the standard output stream.

REPEAT Submit the next request a certain amount of times.

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition

Reprinted for H-P/[email protected], Hewlett Packard Coffing Data Warehousing, Coffing Publishing (c) 2005, Copying Prohibited

Page 23 / 24

Page 24: Chapter 2 BTEQ

8/11/2019 Chapter 2 BTEQ

http://slidepdf.com/reader/full/chapter-2-bteq 24/24

These commands control the formatting for Teradata and present the data in a report mode to the screen or printer.

Figure 2-14  

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.

eradata Utilities: BTEQ, FastLoad, MultiLoad, TPump, and FastExport, Second Edition