Teradata Utilities: BTEQ

Reprinted for KV Satish Kumar, IBM kvskumar@in.ibm.com Reprinted with permission as a subscription benefit of Books24x7, http://www.books24x7.com/

i

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

Logging onto BTEQ "It's choice – not change – that determines your destiny. Users can also submit BTEQ jobs from batch scripts. We will talk about these in future chapters. Interactive users can submit SQL and receive an answer set on the screen. Coffing Publishing (c) 2005.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 the original way that SQL was submitted to Teradata as a means of getting an answer set in a desired format. There are other utilities that are faster than BTEQ for importing or exporting data. ♦ Importing Data: Data can be read from a file on either a mainframe or LAN attached computer and used for substitution directly into any Teradata SQL using the INSERT. This allows BTEQ a great deal of flexibility in formatting data. IBM Coffing Data Warehousing. This is the utility that I used for training at Wal-Mart. such as WITH and WITH BY that Queryman has problems in handling. But you can't use that knowledge if you can't log onto the system! This next slide is going to teach you how to logon to BTEQ. AT&T. SQL commands do not ever start with a period and they must always be terminated with a semi-colon. ♦ Exporting Data: Data can be written to either a mainframe or LAN attached computer using a SELECT from Teradata.ibm. Here is what is excellent about BTEQ: • BTEQ can be used to submit SQL in either a batch or interactive environment.Chapter 2: BTEQ "Civilization advances by extending the number of important operations which we can perform without thinking about them. you've chosen to arm yourself with an unlimited arsenal of knowledge. and allow for the work to be done in the background. and SouthWestern Bell back in the early 1990's.com.) and do not require a semi-colon (. • BTEQ is often used to submit SQL. BTEQ is often referred to as the Basic TEradata Query and is still used today as an effective tool. • BTEQ outputs a report format." . creating headings. UPDATE or DELETE statements. Reprinted for ibmkvskumar@in. but BTEQ is still used for smaller jobs. BTEQ commands begin with a period (. but is also an excellent tool for importing and exporting data. You can also pick the format you desire ranging from data files to printed reports to spread sheet formats. Anthem Blue Cross and Blue Shield. and utilizing Teradata extensions. have error checking and conditional logic. Remember that you will be prompted for the password since it's an interactive interface.) to end the statement." – Jean Nidetch By taking a chance in this industry. Copying Prohibited . where Queryman outputs data in a format more like a spreadsheet.

Depending on the configuration of your Teradata DBS. Coffing Publishing (c) 2005. so don't forget to put one at the end of your query. Second Edition 2 Let's logon to BTEQ and show all information in the Employee_Table: Before you can use BTEQ. Some systems may also require additional user identification codes depending on company standards and operational procedures.50 ? 54500. and FastExport. IBM Coffing Data Warehousing. you must have user access rights to the client system and privileges to the Teradata DBS.ibm.00 400 48000.com.00 400 Figure 2-1 Reprinted for ibmkvskumar@in.00 200 54500. FastLoad. Employee_Table Employee_No Last_Name 2000000 Jones 1256349 Harrison 1333454 Smith 1121334 Strickling First_Name Squiggy Herbert John Cletus Salary Dept_No 32800. Below is an example of a Teradata table to demonstrate BTEQ operations. MultiLoad. Using BTEQ to Submit Queries Submitting SQL in BTEQ's Interactive Mode Once you logon to Teradata through BTEQ. Copying Prohibited . you are ready to run your queries. Teradata knows the SQL is finished when it finds a semi-colon.Teradata Utilities: BTEQ. TPump. Normal system access privileges include a user ID and a password. you may need to include an account identifier (acctid) and/or a Teradata Director Program Identifier (TDPID).

ibm. BTEQ has the ability to use WITH BY statements: Reprinted for ibmkvskumar@in. 5 Columns returned. The result set Figure 2-2 WITH BY Statement "Time is the best teacher. FastLoad. 2 rows found. Then enter PASSWORD at the second prompt.00 54500.com.LOGON cdw/sql01 Password: XXXXX Enter your BTEQ/SQL Request or BTEQ Command. Employee_No Last_name 1256349 Harrison 1121334 Strickling First_name Herbert Cletus Salary 54500. it kills all of its students.Teradata Utilities: BTEQ. TPump.00 Dept_No 400 400 Type at command prompt: Logon with TDPID and USERNAME. We can use the WITH BY statement in BTEQ. *** Query Completed. IBM Coffing Data Warehousing. *** Total elapsed time was 1 second. BTEQ will respond and is waiting for a command. Copying Prohibited ." – Robin Williams Investing time in Teradata can be a killer move for your career. whereas we cannot use it with Nexus or SQL Assistant. Second Edition 3 BTEQ execution . SELECT * FROM Employee_Table WHERE Dept_No = 400. The WITH BY statement works like a correlated subquery in the fact that you can us aggregates based on a distinct column value. MultiLoad. An SQL Statement BTEQ displays information about the answer set. but unfortunately. and FastExport. Coffing Publishing (c) 2005.

Second Edition 4 "I've learned that you can't have everything and do everything at the same time. and FastExport. Copying Prohibited . MultiLoad. We can get a grand total or an overall average with the WITH statement. FastLoad.com. TPump.ibm. just leave out BY. Coffing Publishing (c) 2005.Teradata Utilities: BTEQ." – 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. IBM Coffing Data Warehousing. Here's a good example: Using WITH on a whole column: Reprinted for ibmkvskumar@in.

com. Victor did seem more worried about the hunchback than the rollback. which isn't the case in Nexus or SQL assistant. Second Edition 5 Transactions in Teradata Mode "He who every morning plans the transaction of the day and follows out that plan. However. TPump. and FastExport.ibm. FastLoad. MultiLoad.Teradata Utilities: BTEQ.). Turning your queries into a single transaction is often the best plan. but can sometimes make one Miserables. We can use the BT/ET keywords to bundle several queries into one transaction. Coffing Publishing (c) 2005. You also need to end every query with a semi-colon (. 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. Copying Prohibited . Often in Teradata we'll see multiple queries within the same transaction. IBM Coffing Data Warehousing. we're going to put four single statements into a single transaction Reprinted for ibmkvskumar@in." . For example: In Teradata mode.

and FastExport. Second Edition 6 "What is defeat? Nothing but education." – Wendell Phillips The final query in our last transaction is what caused our updates to fail.com. FastLoad. nothing but the first step to something better. you're telling Teradata that when it comes to committing. we either want all or none. TPump. but instead the smell of de Feet! Actually. IBM Coffing Data Warehousing. it really was an education leading to something better. Coffing Publishing (c) 2005. The last query in our set did not work: Now let's take a look at the Employee_Table: Reprinted for ibmkvskumar@in. Copying Prohibited . Make sure that your syntax is correct when using the method of BT and ET because a mistake causes a massive rollback. This was not the sweet taste of victory.Teradata Utilities: BTEQ. Since our last query in the transaction failed the Transient Journal rolled back all the queries in our entire transaction.ibm. When using BT/ET in your transaction. MultiLoad.

Second Edition 7 Our updates didn't work! That's because we bundled all four queries into one transaction. FastLoad. Here's another way to set up a bundle of queries into one transaction. How many queries are parts of the same transaction below? Four! Another way to perform a multi-statement transaction in Teradata mode: Placing the semi-colon at the beginning of the next line (followed by another statement) will bundle those statements together as one transaction.ibm. Remember that the semi-colon must be at the very beginning of the next line for a query to be considered as part of the same transaction. and FastExport. sometimes we have to do what's required. MultiLoad. Since our last query failed. Because we are in Teradata mode if any query fails then all queries that are part of the same transaction roll back. Notice that our Employee_Table was not updated.com." – Sir Winston Churchill Sometimes we're required to use an alternative method to get the job done if we want to win like Winston. the tables were rolled back to their original state before the transaction took place. IBM Coffing Data Warehousing. Coffing Publishing (c) 2005. Alternative Transactions in Teradata Mode "It's not enough that we do our best. Notice where we place the semi-colon in our queries and you will understand this technique. TPump.Teradata Utilities: BTEQ. Copying Prohibited . Reprinted for ibmkvskumar@in.

Copying Prohibited .set session transaction ANSI' and be sure to do it before you actually logon to BTEQ. Second Edition 8 just like in the first example. ALTER. Let's log back onto BTEQ. Transactions in ANSI Mode "The man who views the world at 50 the same as he did at 20 has wasted 30 years of his life. All queries in ANSI mode will also work in Teradata mode and vice versa. simply type '. but this time change it to ANSI mode: "Be not afraid of growing slowly. be afraid only of standing still.ibm. FastLoad. However.com. DROP.Teradata Utilities: BTEQ. It will fail even though we have 'Mike' 'Larkins' in our table. Those things are how case sensitivity is handled. To change to ANSI mode. how transactions are committed and rolled back. This tells Teradata to commit to what's been done. three things will be different in ANSI mode versus Teradata mode. you can logon like always. but this time you will be in ANSI mode. This is because ANSI is case sensitive and we did not capitalize the 'L' in 'Larkins'. Coffing Publishing (c) 2005. Let's run a few queries in ANSI mode: Reprinted for ibmkvskumar@in. DATABASE) we have to use the 'commit' command immediately." – Muhammad Ali ANSI (American National Standard Institution) allows us to view the same queries in a different way. and how truncation is accepted. in ANSI mode after any DDL statement (CREATE. MultiLoad. Then. IBM Coffing Data Warehousing. Our query below will attempt to find anyone with a last_name of 'larkins'. TPump." -Chinese Proverb Remember the first rule of ANSI mode: all transactions must be committed by the user actually using the word 'COMMIT'. and FastExport. Also.

FastLoad." – Albert Einstein The Rollback keyword is the SQL mulligan of Teradata. IBM Coffing Data Warehousing. Rollback will erase any changes made to a table. This can be very useful if something didn't work. Not keeping rollback in your arsenal would be insane. TPump. Coffing Publishing (c) 2005. and FastExport. MultiLoad. you cannot rollback once you've used the commit keyword. We even have to COMMIT after setting our DATABASE or we will get an error. Second Edition 9 Notice that we have to COMMIT after any DDL or Update before the transaction is committed. but we know there's a Mike Larkins within the table. Reprinted for ibmkvskumar@in.ibm. We didn't have any rows return.Teradata Utilities: BTEQ. Copying Prohibited . Rollback "Insanity: doing the same thing over and over again and expecting different results. Change 'larkins' to 'Larkins'. That's because BTEQ is case sensitive.com. However.

MultiLoad. Using commit will ensure that your successes aren't hidden by your failures. Coffing Publishing (c) 2005. FastLoad.Teradata Utilities: BTEQ. Copying Prohibited . the rest won't be rolled back to their original state. IBM Coffing Data Warehousing. Second Edition 10 Advantages to ANSI Mode ANSI mode is great because when you bundle several queries into one transaction and one doesn't work.com.ibm. Now notice that I will have multiple statements in the same transaction and that I purposely fail the last SQL statement: Reprinted for ibmkvskumar@in. TPump. and FastExport.

TPump. IBM Coffing Data Warehousing. The Employee_Table was updated! In ANSI mode. then I'm sure you're curious as to how we can fix the situation.Teradata Utilities: BTEQ. There is no cure for curiosity. DOS." – Galileo Galilei Discovering the advantages in using ANSI will only make SQL easier to write. do the following: Reprinted for ibmkvskumar@in. Coffing Publishing (c) 2005. and FastExport. NCR MP-RAS. FastLoad. Hewlett-Packard HP-UX. only failed transactions are rolled back when it comes to multi-statement transactions. Batch scripting allows us to write out pages and pages of queries and execute those queries in one single swoop. Sun Solaris). To submit a job in batch mode. Copying Prohibited .com. the point is to discover them. Microsoft Windows and OS/2 operating systems." – Dorothy Parker If you've been bored waiting for your queries to finish. Creating a Batch Script for BTEQ "The cure for boredom is curiosity.ibm. It might take a little bit more typing. MultiLoad. BTEQ can also run in batch mode under UNIX (IBM AIX. Second Edition 11 Which statements were rolled back? "All truths are easy to understand once they are discovered. but a little work now can save you lots of time later. Macintosh.

Type in the location and output file name. I recommend calling it 'BTEQ_First_Batch_Script. MultiLoad. Second Edition 12 1. we have to access the BTEQ utility via dos prompt. Type in the input file name 3. as long as you can remember what you named it and where you saved it. To run our new batch script. The following example shows how to create a batch script and how to invoke the script using BTEQ from a DOS command. and follow the steps below: Let's run our query in Batch! Reprinted for ibmkvskumar@in. I fear the lack of them. FastLoad. Simply open up notepad and type in the following. TPump. Simply use command prompt to access the utility. your password will not be prompted. and FastExport. Invoke BTEQ (using dos prompt) 2.Teradata Utilities: BTEQ.ibm. you'll be fine. Be sure that you save it as a . then save it. Coffing Publishing (c) 2005.txt file.logoff so that your queries aren't left hanging.quit or . Be sure to end with either a .txt' and save it in the C:\Temp folder. However.com. Instead. just add the password after your login name. and a comma separates the two. IBM Coffing Data Warehousing. Using Batch scripting with BTEQ Running your Batch Script in BTEQ "I do not fear computers. When using Batch scripting. Copying Prohibited ." – Isaac Asimov The BTEQ utility enables us to run our scripts in batch mode.

txt'. BTEQ will automatically open in DOS. BTEQ will ensure that the steps it takes will be big enough to get the job done. Coffing Publishing (c) 2005. Our results are returned Interactively Reprinted for ibmkvskumar@in. and then it will access the file from the location you listed. FastLoad. then hit enter.ibm. Copying Prohibited . TPump. MultiLoad. You can't cross a chasm in two small steps.Teradata Utilities: BTEQ. Results from a BTEQ Batch Script "Don't be afraid to take a big step when one is indicated.com. type in the following: 'BTEQ < c:\temp\BTEQ_First_Script. Second Edition 13 Once you're in DOS. and FastExport. Whether you're accessing a small table or crossing over a chasm of information." -David Lloyd George BTEQ will run your query in steps to produce the answer you're looking for. IBM Coffing Data Warehousing.

the contents of a file called "mylogon. TPump. To submit results to two different files at once. IBM Coffing Data Warehousing. the '>BTEQ_First_Export.com. Copying Prohibited . Then. Since putting password information into a script is scary for security reasons. It is generally recommended and a common practice to store the logon and password in a separate file that that can be secured. Reprinted for ibmkvskumar@in. the script should contain the following command instead of a . Second Edition 14 Placing our BTEQ Output to a file "The secret to creativity is knowing how to hide your sources. Coffing Publishing (c) 2005. inserting the password directly into a script that is to be processed in batch mode may not be a good idea. Then.whynot'." – Arthur Koestler Discovering how easy it is to export your data in batch mode is a key step in learning Teradata utilities.LOGON cdw/sql00. We can export our results in batch as well Notice that the BTEQ command is immediately followed by the '<BTEQ_First_Script. MultiLoad. FastLoad. 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.Teradata Utilities: BTEQ. including the original query and what BTEQ did to generate its answer set. the more obvious it seems afterwards." – Albert Einstein We can use BTEQ to export our results to another text document. Here are our results. Simply go to the folder where you saved the exported data (the previous examples saved the file as c:\temp\BTEQ_First_Export.txt' names the file where the output messages are written.txt' to tell BTEQ which file contains the commands to execute.LOGON: .txt" might be: '.txt. For example. it is not in the script for anyone to see.txt). and FastExport.ibm. That way.RUN FILE=c:\temp\mylogon. Exporting data also works very well when you're trying to document your query along with the results.

FastLoad. IBM BTEQ conditional logic that will check to ensure that the delete worked or if the table even existed. The Label INSEMPS provides code so Coffing Data Warehousing.ibm. TPump. and FastExport. However. Reprinted for ibmkvskumar@in.IF ERRORCODE = 0 THEN .com. then BTEQ will create it.Teradata Utilities: BTEQ. If the table does not exist. Copying Prohibited .txt DATABASE SQL_Class.Steven Wright Below is a BTEQ batch script example. If the table did not exist. the BTEQ conditional logic will instruct Teradata to create it. . the database. and then delete all the rows from the Employee_Table.RUN FILE = c:\temp\mylogon. */ CREATE TABLE Employee_Table (Employee_No INTEGER. MultiLoad. Coffing Publishing (c) 2005. Second Edition 15 What you'll find in our new text document Using BTEQ Conditional Logic "I was walking down the street wearing glasses when the prescription ran out.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. then Teradata will move forward and insert data. DELETE FROM Employee_Table. the left panel contains BTEQ base commands and the right panel provides a brief description of each command. If the table does exist." . the Create table step will be skipped and directly GOTO INSEMPS. Note: In script examples. if the table already exists. The initial steps of the script will establish the logon.

2). 'Herbert'. the BTEQ export function has several export formats that a user can choose from depending on the desired output.00. The information on Reprinted for ibmkvskumar@in. These characteristics include: field mode. Copying Prohibited . Once the table . which is why it's still considered the future of data warehousing. MultiLoad. The flat files we create are merely used to store information contained within a table. users will export data to a flat file format that is composed of a variety of characteristics. In addition. 'Mandee'. Dept_No SMALLINT) UNIQUE PRIMARY INDEX (Employee_No). P.LABEL INSEMPS[*] has been created.QUIT empty table. Salary DECIMAL(8. they do things differently there. insert the two new rows into the . 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. 'Chambers'. the BTEQ Logic can go directly to inserting records into the Employee_Table. TPump. indicator mode. Generally. Using BTEQ to Export Data "The trouble with facts is that there are so many of them. BTEQ allows data to be exported directly from Teradata to a file on a mainframe or network-attached computer. 48850. [*]Both labels have to be identical or it will not work. IBM Coffing Data Warehousing. and that's a fact. Hartley Transferring data from table to another without the use of a flat file is a thing of the past." L. Teradata will then 100). 400). or dif mode. FastLoad.ibm. INSERT INTO Employee_Table (1232578.00. Coffing Publishing (c) 2005. and FastExport. INSERT INTO Employee_Table (1256349.com. First_name CHAR(12).Teradata Utilities: BTEQ. Teradata does things differently now. Second Edition 16 Last_name CHAR(20)." – Samuel McChord Crothers Creating flat files is one of the most important tasks in Teradata. 54500. 'Harrison'.

If this data is simply loaded into another RDBMS. every system uses a zero for a numeric NULL and a space or blank for a character NULL. FastLoad. Field Mode (also called REPORT mode): This is set by . but also provides host operating systems with the means of recognizing missing or unknown data (NULL) fields. this means that INTEGER data is written as a 4-byte binary field. This bitmap contains one bit per field/column. expanded packed or binary data (for humans to read) and can be understood using a text editor.EXPORT REPORT. white space.Teradata Utilities: BTEQ.txt What our flat file looks like: And I thought French was tough. This is the default mode for BTEQ and brings the data back as if it was a standard SQL SELECT statement. For example. The issue is that there is no standard character defined to represent either a numeric or character NULL. 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. This mode writes the data in data mode.ibm. the bit remains a zero. and FastExport. if the data is not NULL. but it is perfectly written. there are no headers or white space between the data contained in each column and the data is written to the file (e. MultiLoad. This will bring data back as a flat file. Coffing Publishing (c) 2005. Second Edition 17 these files is written in binary code.EXPORT INDICDATA.. Indicator Mode: This is set by . Executing our fastload_creating_flatfile01. When a Teradata column contains a NULL. Therefore.g. Reprinted for ibmkvskumar@in. but a zero or space. TPump. INDICATA puts a bitmap at the front of every record written to the disk. Each parcel will contain a complete record. Since it is not a report. This is important if the data is to be loaded into another Relational Database System (RDBMS). It may look garbled. disk drive file) in native format. IBM Coffing Data Warehousing. it is no longer a NULL. it cannot be read and understood using a normal text editor. the bit for that field is turned on by setting it to a "1". Likewise. Copying Prohibited .com. We will be able to use this flat file for future 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 . When we Fastload the data back to a table it will look beautiful. which is why the text seems garbled. The output of this BTEQ export would return the column headers for the fields. So. To remedy this situation.EXPORT DATA.

MultiLoad. Second Edition 18 Therefore. However. This means that from one to eight columns being referenced in the SELECT will add one byte to the length of the record. then the command is an SQL command. 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. you must account for these bits when defining the LRECL in the Job Control Language (JCL). With this being stated. When executing on non-mainframe systems. BTEQ EXPORT Example Using Record (DATA) Mode The following is an example that displays how to utilize the export Record (DATA) option. there is one indicator bit per field selected. Copying Prohibited . 2 bytes are added even though only nine bits are needed.com. FoxPro and Lotus. this internal processing is automatic and potentially important. your length is too short and the job will end with an error. As mentioned earlier. IBM Coffing Data Warehousing. the record length is automatically maintained. The output of the exported data will be a flat file. Therefore. LAN) computer. This might be handy in a test environment to stop BTEQ before the end of transferring rows to the file. not bits. However. Reprinted for ibmkvskumar@in.EXPORT DATA DDNAME = data definition statement name (JCL) syntax: • LAN syntax: . When doing an export on a mainframe or a network-attached (e. there is one primary difference in the . for nine columns selected. the LRECL becomes 1 byte longer and must be added.) at the beginning some of script lines. In other words. if one bit is needed a minimum of eight (8 bits per byte) are allocated. If there is no period. However. Therefore. which allows users to export data from Teradata to be directly utilized for spreadsheet applications like Excel. for every eight fields. To determine the correct length.Teradata Utilities: BTEQ. FastLoad. Yet. Again. Therefore. when exporting to a mainframe. the output record will be two bytes longer. TPump. Notice the periods (. INDICDATA mode will automatically allocate eight of them. it must be imported as DATA and the same is true for INDICDATA. When selecting nine to sixteen columns. the following information is important. Otherwise. being consistent is our only responsibility. where appropriate. on a mainframe system. and FastExport. DIF Mode: Known as Data Interchange Format. the JCL (LRECL) must account for this additional 2 bytes in the length.EXPORT DATA FILE = actual file name The following example uses a Record (DATA) Mode format. The optional LIMIT is to tell BTEQ to stop returning rows after a specific number (n) of rows. However.ibm. it becomes imperative that you be consistent.. INDICDATA mode gives the Host computer the ability to allocate bits in the form of a byte. Coffing Publishing (c) 2005. A period starting a line indicates a BTEQ command. computers allocate data in bytes. they are the fastest method of transferring data.g. When it is exported as DATA.EXPORT command. on a network-attached system. Since both DATA and INDICDATA store each column on disk in native format with known lengths and characteristics. The difference is the following: • Mainframe . if one bit is required by the host system. one bit is needed per field output onto disk.

Coffing Publishing (c) 2005. the following report will be generated on disk. Copying Prohibited Reprinted for ibmkvskumar@in. if there is no period.EXPORT REPORT FILE = C:\EMPS.00 Dept_No ? 400 200 400 Logon to TERADATA This Export statement will be in record (DATA) mode.whynot.00 400 48000. Figure 2-7 After this script has completed. FastLoad.EXPORT DATA FILE = C:\EMPS.QUIT Reverse previous export command and fall through to Done.com.88 200 36000.00 400 41888.00 400 56177.LABEL Done .GOTO Done more rows – else GOTO Done. This Export statement will be in field (REPORT) mode.LOGON CDW/sql01.TXT SELECT * FROM SQL_Class.00 54500. 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.ibm. MultiLoad.Teradata Utilities: BTEQ.) at the beginning some of script lines. IBM . A period starting a line indicates a BTEQ command and needs no semi-colon. TPump. DATABASE SQL_Class.LOGON CDW/sql01. Likewise.EXPORT RESET . . BTEQ checks to ensure no errors occurred and selects .QUIT 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. Second Edition 19 Employee_Table Employee_No Last_Name 2000000 Jones 1256349 Harrison 1333454 Smith 1121334 Strickling .50 100 Coffing Data Warehousing. Logon to TERADATA .TXT file will be created as a report. The EMPS.whynot. . .50 54500.TXT file will be created as a flat file Finish the execution. First_Name Squiggy Herbert John Cletus Salary 32800. . and FastExport.IF ERRORCODE > 0 THEN .00 200 54500.Employee_Table. then the command is an SQL command and requires a semi-colon. Notice the periods (. SELECT * FROM Department_Table.TXT SELECT * FROM Employee_Table. The EMPS.00 48000.

Second Edition 20 1000234 2312225 Smythe Larkins Richard Loraine 64300. FastLoad.Teradata Utilities: BTEQ. Set the format parameters for the final report . TPump.00 54500.00 48000. As a user gains experience with BTEQ export.EXPORT REPORT FILE = C:\EMP_REPORT.LOGON CDW/sql01. a number of BTEQ commands were added to the export script. BTEQ export works in the same manner.QUIT Figure 2-8 After then following script has been completed. Employee Number Employee Profiles Last Name First Name Squiggy Herbert John Cletus Billy William Mandee Richard Loraine Salary 32800. Dept_No AS "Department Number" Employee_Table. aliases and a title have been added to the script. So I built another space ship and constructed another launching station. . I was not satisfied with the design and decided to make changes. the following report will be generated on disk.whynot.00 56177.com.50 54500. when I was done. IBM Coffing Data Warehousing.00 10 300 I remember when my mom and dad purchased my first Lego set. Below is a review of those commands. With that being said. Logon to TERADATA . Salary AS "Salary".50 64300. In addition. Notice that the columns have an alias.SET WIDTH 90 .TXT Last_name AS "Last Name". Specifies the columns that are being selected. the following is an example that displays a more robust example of utilizing the Field (Report) option.SET FORMAT ON . and proceeded to follow the instructions to complete the station. Copying Prohibited . First_name AS "First Name". Coffing Publishing (c) 2005. The output of the exported data will appear like a standard output of a SQL SELECT statement.88 36000. The EMP_REPORT.00 Department Number ? 400 200 400 200 400 100 10 300 2000000 Jones 1256349 Harrison 1333454 Smith 1121334 Strickling 1324657 Coffing 2341218 Reilly 1232578 Chambers 1000234 Smythe 2312225 Larkins From the above example. SELECT Employee_No AS "Employee Number".TXT file will be created as a report. and FastExport.ibm.EXPORT RESET . the easier it will get to work with the utility. I was so excited about building my first space station that I ripped the box open.00 41888.00 40200.SET HEADING 'Employee Profiles' This Export statement will be in field (REPORT) mode. Reprinted for ibmkvskumar@in. FROM Reverse previous export command effects . This example will export data in Field (Report) Mode format.00 40200. However. MultiLoad.

) at the beginning some of script lines. the name and record description of the file must be known ahead of time. SKIP = 2 . Notice the periods (. :L_name.LOGON CDW/sql01. The input of the imported data will populate the Employee_Table.REPEAT * USING Emp_No L_name F_name Salary Dept_No (INTEGER).TXT".QUIET ON . maybe the job started and loaded a few rows into the table with a UPI defined. :F_name. These will be defined within the script file. you can skip over them using this option. INSERT INTO Employee_Table (Employee_No. • The HEADING command specifies a header that will appear at the top every page of a report. Limit messages out. (CHAR(20)). First_name. (SMALLINT) Specify DATA mode. Salary.IMPORT DATA FILE = C:\EMPS. Format of the IMPORT command: . Coffing Publishing (c) 2005. The USING Specifies the field in the input file and names them. :Salary.ibm. (DECIMAL(8. name the file to read "EMPS. So.com.TXT.whynot.SESSIONS 4 . . Last_name. but skip the first 2 records. Reprinted for ibmkvskumar@in. and FastExport. a mainframe tape may have header records that should not be processed. The SKIP option is used when you wish to bypass the first records in a file. Copying Prohibited .SKIP=n] The script below introduces the IMPORT command with the Record (DATA) option. 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 more tables. then the command is an SQL command. Other times. Specify the insert parameters for the employee_table Substitutes data from the fields into the SQL command. FastLoad. :Dept_No) . For example. MultiLoad. Second Edition 21 • The WIDTH specifies the width of screen displays and printed reports.IMPORT <mode> { FILE | DNAME } = <filename> [. TPump.2)). Specify the number of SESSIONS to establish with Teradata Logon to TERADATA . IBM Coffing Data Warehousing. • The FORMAT command allows the ability to enable/inhibit the page-oriented format option. A period starting a line indicates a BTEQ command. If there is no period. Dept_No) VALUES (:Emp_No. based on characters per line. Loop in this script until end of records in file.Teradata Utilities: BTEQ. (VARCHAR(12)). The following example will use a Record (DATA) Mode format. Loading them again will cause an error. In order to do this processing.

The following builds upon the IMPORT Record (DATA) example above. this script adds a CREATE TABLE statement. The default is one record.QUIT Exit the script after all data read and rows inserted. else forgetting the E becomes . Note: Be careful how you spell . INSERT INTO Employee_Profile (Employee_No. Using REPEAT 10 would perform the loop 10 times. • .SESSIONS 2 . . .com. Last_name. Dept_No) Reprinted for ibmkvskumar@in.QUIET. The next page contains a review of those commands. The example below will still utilize the Record (DATA) Mode format. Dept_No SMALLINT ) UNIQUE PRIMARY INDEX (Employee_No) .ibm.REPEAT * causes BTEQ to read a specified number of records or until EOF. (CHAR(20)). First_name VARCHAR(12). • . In addition.whynot.QUIET ON . This causes BTEQ to read the first 120 records from the file. ( Employee_No INTEGER.TXT .IMPORT INDICDATA FILE = C:\IND-EMPS.Teradata Utilities: BTEQ. MultiLoad. However.QUIT and it will. IBM The USING Specifies the parameters of the input file. a number of BTEQ commands were added to the import script.QUIET ON limits BTEQ output to reporting only errors and request processing statistics. Specify the number of SESSIONS to establish with Teradata Logon to TERADATA Make the default database SQL_Class DATABASE SQL_Class.2)). and FastExport. Salary DECIMAL(8. This statement will create the Employee_Profile CREATE TABLE Employee_Profile table. (SMALLINT) This import statement specifies INDICDATA mode. The input file is from a LAN file called IND-EMPS. FastLoad.REPEAT 120 USING Employee_No Last_name First_name Salary Dept_No (INTEGER). (VARCHAR(12)). (DECIMAL(8. Copying Prohibited . Last_name CHAR(20). Salary. the imported data will populate the newly created Employee_Profile table. Figure 2-9 From the above example. TPump. • The USING defines the input data fields and their associated data types coming from the host. Coffing Publishing (c) 2005.LOGON CDW/sql01. Second Edition 22 . First_name. Quiet on limits the output to reporting only errors and processing statistics.2).TXT. Coffing Data Warehousing.

QUIT Figure 2-10 Notice that some of the scripts have a .LOGOFF . Specify the insert parameters for the employee_profile. Second Edition 23 VALUES (:Employee_No. There are three issues involving record lengths and they are: • Fixed columns • Variable columns • NULL indicators Fixed Length Columns: For fixed length columns you merely count the length of the column. :First_name. Substitute the values to be inputted into the SQL command. :Salary. MultiLoad. such as IBM mainframes.com. :Last_name.QUIT. the session is terminated. Coffing Publishing (c) 2005.LOGOFF is optional because when BTEQ quits. Copying Prohibited . :Dept_No) . TPump. and FastExport. require the correct LRECL (Logical Record Length) parameter in the JCL. The following page will discuss how to figure out the record lengths. A logoff makes it a friendly departure and also allows you to logon with a different user name and password. 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 ibmkvskumar@in.LOGOFF and . FastLoad.Teradata Utilities: BTEQ. . IBM Coffing Data Warehousing.ibm. and will abort if the value is incorrect. The . Determining Out Record Lengths Some hosts.

• 08 User error. total digits / 2 +1) • DECIMAL(7. BTEQ Return Codes Return codes are two-digit values that BTEQ returns to the user after completing each job or task.QUIT 15 Reprinted for ibmkvskumar@in. IBM Coffing Data Warehousing. In reality you can save much space because trailing blanks are not kept.com. Coffing Publishing (c) 2005. 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. Copying Prohibited . This might be handy for debugging purposes. If your record has 9-16 fields. 10 bytes • VARCHAR(8) 12 bytes • VARCHAR(10) Indicator columns: As explained earlier.2) 7 bytes • DECIMAL(12.2) Variable columns: Variable length columns should be calculated as the maximum value plus two. and FastExport. The error code or "return code" can be any number you specify using one of the following: Override Code Description • . FastLoad. • 02 User alert to log on to the Teradata DBS. • 12 Severe internal error. Second Edition 24 4 bytes (packed data. the indicators utilize a single bit for each field. The two bytes are for the number of bytes for the binary length of the field. MultiLoad.Teradata Utilities: BTEQ. TPump. You can over-ride the standard error codes at the time you terminate BTEQ. then you add one extra byte to the total length of all the fields. The logical record will assume the maximum and add two bytes as a length field per column.ibm. • 04 Warning error. If your record has 8 fields (which require 8 bits). then add two bytes.

Coffing Publishing (c) 2005. IBM Coffing Data Warehousing. CHARSET SESSION Specifies a disposition of warnings issued in response to violations of ANSI SQLFLAG syntax. SESSION Specifies the name of a character set for the current session or sessions. or utility must LOGON to Teradata to establish a session. However. SECURITY Specifies the security level of messages between a network-attached system and the Teradata Database.ibm. these 60 different BTEQ commands are utilized in four areas. CONTROLS SHOW VERSIONS Displays the BTEQ software release versions. and FastExport. HALT EXECUTION Abort any and all active running requests and transactions and EXIT BTEQ. TPump. SESSIONS Specifies the number of sessions to use with the next LOGON command. Every user. SESSION Specifies whether transaction boundaries are determined by Teradata SQL TRANSACTION or ANSI SQL semantics.EXIT 15 BTEQ Commands The BTEQ commands in Teradata are designed for flexibility. FastLoad.com. and NONE. • Session Control Commands • File Control Commands • Sequence Control Commands • Format Control Commands Session Control Commands ABORT Abort any and all active running requests and transactions for a session. SHOW Displays all of the BTEQ control command options currently configured. LOGOFF End the current session or sessions. These commands are not used directly on the data inside the tables. but a warning message will be provided. Second Edition 25 • . but do not exit BTEQ. LOGON Starts a BTEQ Session. The four settings are FULL. EXIT Immediately end the current session or sessions and exit BTEQ.Teradata Utilities: BTEQ. This will utilize the default configurations. QUIT End the current session or sessions and exit BTEQ. MultiLoad. Copying Prohibited . INTERMEDIATE. The SQL will still run. application. TDP Used to specify the correct Teradata server for logons for a particular session. Figure 2-11 Reprinted for ibmkvskumar@in. ENTRY. but do not exit BTEQ. DEFAULTS Reset all BTEQ Format command options to their defaults.

One of multiple data mode options for data selected from Teradata.ibm. and FastExport. Open a file with a specific format to import information into Teradata. Reprinted for ibmkvskumar@in.com. Second Edition 26 File Control Commands These BTEQ commands are used to specify the formatting parameters of incoming and outgoing information. Enable/inhibit the page-oriented format command options. Abort any and all active running requests and transactions and EXIT BTEQ. The GOTO command will always GO directly TO a particular line of code based on a label. Specifies a maximum allowable error severity level. End the current session or sessions and exit BTEQ. IBM Coffing Data Warehousing. Execute an MS-DOS. Skip all intervening commands and resume after branching forward to the specified label. PC-DOS. MultiLoad. Execute an MVS TSO command from inside the BTEQ environment. Test a stated condition. and then resume processing based on the test results. FIELD. Format Control Commands These commands control the formatting for Teradata and present the data in a report mode to the screen or printer. or RECORD MODE. FIELD. Coffing Publishing (c) 2005.Teradata Utilities: BTEQ. CMS ERROROUT EXPORT HALT EXECUTION FORMAT IMPORT INDICDATA OS QUIET RECORDMODE REPEAT RUN TSO Figure 2-12 Execute a VM CMS command inside the BTEQ environment. Write error messages to a specific output file. Open a file with a specific format to transfer information directly from the Teradata database. Place a comment on the standard output stream. Submit the next request a certain amount of times Execute Teradata SQL requests and BTEQ commands directly from a specified run file. Limit BTEQ output displays to all error messages and request processing statistics. FastLoad. or RECORD). Copying Prohibited . One of multiple data mode options for data selected from Teradata. (INDICDATA. This includes identifying sources and determining I/O streams. Sequence Control Commands These commands control the sequence in which Teradata commands operate. or UNIX command from inside BTEQ. End the current session or sessions and exit BTEQ. ABORT ERRORLEVEL EXIT GOTO HANG IF…THEN LABEL MAXERROR QUIT REMARK REPEAT Figure 2-13 Abort any active transactions and requests. Submit the next request a certain amount of times. The modes are INDICDATA. TPump. Pause BTEQ processing for a specific amount of time. Assign severity levels to particular error numbers.

SHOWCONTROLS Displays all of the BTEQ control command options currently configured. RETRY Retry requests that fail under specific error conditions. RETLIMIT Specifies the maximum number of rows to be displayed or written from a Teradata SQL request. and FastExport. FIELD. PAGEBREAK Ejects a page whenever a specified column changes values. QUIET Limit BTEQ output displays to all error messages and request processing statistics. TITLEDASHES Display dash characters before each report line summarized by a WITH clause. PAGELENGTH Specifies the page length of printed reports based on lines per page. FOLDLINE Split or fold each line of a report into multiple lines. This will utilize the default configurations. SIDETITLES Place titles to the left or side of the report instead of on top. (INDICDATA. Coffing Publishing (c) 2005. SUPPRESS Replace each and every consecutively repeated value with completely-blank character strings. Figure 2-14 Reprinted for ibmkvskumar@in. TPump. RTITLE Specify a header appearing at the top of all pages of a report. SKIPLINE Inserts blank lines in a report when the value of a column changes specified values.com. MultiLoad. Second Edition 27 DEFAULTS Reset all BTEQ Format command options to their defaults. UNDERLINE Display a row of dash characters when the specified column changes values. ECHOREQ Enable the Echo required function in BTEQ returning a copy of each Teradata SQL request and BTEQ command to the standard output stream. Copying Prohibited .ibm. FastLoad. RETCANCEL Cancel a request when the specified value of the RETLIMIT command option is exceeded. FOOTING Specify a footer to appear at the bottom of every report page.Teradata Utilities: BTEQ. SEPARATOR Specifies a character string or specific width of blank characters separating columns of a report. IMPORT Open a file with a specific format to transfer or IMPORT information directly to Teradata. based on characters per line. IBM Coffing Data Warehousing. OMIT Omit specific columns from a report. RECORDMODE One of multiple data mode options for data selected from Teradata. or RECORD). WIDTH Specifies the width of screen displays and printed reports. EXPORT Open a file with a specific format to transfer information directly from the Teradata database. NULL Specifies a character or string of characters to represent null values returned from Teradata. FORMAT Enable/inhibit the page-oriented format command options.