Documente Academic
Documente Profesional
Documente Cultură
ANSI Teradata
All syntax, both ANSI and Teradata extensions, is accepted in either mode.
BTEQ Examples
Must be entered prior to LOGON. To change session mode, must LOGOFF first.
BT;
UPDATE A … ; UPDATE A … ; UPDATE A … ; (A commits)
UPDATE B … ; UPDATE B … ; UPDATE B … ; (B commits)
COMMIT; ET;
(Both commit) (Both commit)
BT;
UPDATE A … ; UPDATE A … ; (No explicit ROLLBACK
UPDATE B … ; UPDATE B … ; in implicit Txn)
ROLLBACK ; ROLLBACK ;
(Both rollback) (Both rollback)
BT;
UPDATE A … ; UPDATE A … ; UPDATE A … ; (A commits)
UPDATE B … ; UPDATE B … ; UPDATE B … ; (B commits)
LOGOFF; LOGOFF; LOGOFF;
(Both rollback) (Both rollback)
Multi-Statement Requests
UPDATE
UPDATEDept
Dept SET
SETSalary_Change_Date
Salary_Change_Date==CURRENT_DATE
CURRENT_DATE
;;UPDATE
UPDATEManager
Manager SET
SETSalary_Amt
Salary_Amt==Salary_Amt
Salary_Amt**1.06
1.06
;;UPDATE
UPDATEEmployee
Employee SET
SETSalary_Amt
Salary_Amt==Salary_Amt
Salary_Amt**1.04
1.04;;
Teradata mode (NOT CS) ANSI mode – case blind test (CS)
SELECT first_name, last_name SELECT first_name, last_name
FROM Employee FROM Employee
WHERE last_name LIKE '%Ra%'; WHERE UPPER(last_name)
LIKE UPPER('%Ra%');
SQLFLAG setting
BTEQ Example
.SET SESSION SQLFLAG ENTRY; – flags non-core level ANSI syntax
Must be entered prior to LOGON. To change session mode, must LOGOFF first.
For example:
sel date;
$
*** SQL Warning 5821 Built-in values DATE and TIME are not ANSI.
sel date;
$
*** SQL Warning 5804 A FROM clause is required in ANSI Query Specification.
Current Date
-------------------
2004-02-20
HELP SESSION Command
HELP SESSION;
BTEQ Note: To produce this format in
*** Help information returned. One row. BTEQ, use these BTEQ settings:
*** Total elapsed time was 1 second.
.SET SIDETITLES
User Name TFACT04 .SET FOLDLINE
Account Name $M_9038_&D&H
Logon Date 04/02/20
To return to the default settings:
Logon Time 10:43:16
.SET DEFAULTS
Current DataBase TFACT01
Collation ASCII
Character Set ASCII
Transaction Semantics Teradata Notes:
Current DateForm ANSIDate • V2R5 HELP SESSION displays
Session Time Zone 00:00
Default Character Type LATIN
many more parameters than
: : previous releases.
Default Date Format YY/MM/DD • However, to see SQLFLAGGER
: :
Currency Name US Dollars
setting, use SHOW CONTROL
Currency $ command.
: :
Default Timestamp format YYYY-MM-DDBHH:MI:SS.S(F)Z
Current Role TF_Student
Logon Account $M_9038_&D&H
Why A System Calendar?
I’d Like To Know … How does this quarter compare to same quarter last year?
How many shoes do we sell on Sundays vs. Saturdays?
During which week of the month do we sell the most pizzas?
Some Good News Extends properties of DATE data type by joining to Calendar.
Easily joined to other tables, i.e., dimension of a star schema.
High performance - limited I/O.
Has advantages over user-defined calendars.
Standard Usage Statistics are created for materialized table for join planning.
Only necessary rows are materialized for the calendar.
Calendar Table Layout
calendar_date 2003-12-15
day_of_week 2 December 2003
day_of_month 15
S M T W T F S
day_of_year 349 1 2 3 4 5 6
day_of_calendar 37969 7 8 9 10 11 12 13
weekday_of_month 3 14 15 16 17 18 19 20
week_of_month 2 21 22 23 24 25 26 27
week_of_year 50 28 29 30 31
week_of_calendar 5424
month_of_quarter 3
month_of_year 12
month_of_calendar 1248
quarter_of_year 4
quarter_of_calendar 416
year_of_calendar 2003
Derived Tables
• Local to the query (table and columns are named within query)
• Incorporated into SQL query syntax (populated in query via SELECT in FROM)
• Materialized in SPOOL – Spool rows are discarded when query finishes
• No data dictionary involvement
• Commonly used with aggregation
Volatile Tables
• Local to a session – uses SPOOL space
• Uses CREATE VOLATILE TABLE syntax
• Discarded automatically at session end
• No data dictionary involvement
Multiple • Each session can use the same VT name (local to session).
Sessions: • VT name cannot duplicate existing object name for this user.
– Perm or Temp table names
– View names
– Macro names
– Trigger names
FALLBACK: Electable but not often useful for VTs. VTs don’t survive a system reset.
Global Temporary Tables Each instance of global temp table is local to a session.
are similar to Volatile Tables: Materialized tables are dropped automatically at session
end.
Have LOG and ON COMMIT PRESERVE/DELETE options.
Materialized table contents aren’t sharable with other
sessions.
Global Temporary Tables are Base definition is permanent and kept in DD.
different from Volatile Tables: Requires DML privileges necessary to materialize the
table.
Space is charged against an allocation of “temporary
space” - CREATE USER TEMPORARY parameter.
User can materialize up to 32 global tables per session.
Tables can survive a system restart.
Creating Global Temporary Tables
CREATE GLOBAL TEMPORARY TABLE gt_deptsal Base table definition stored in DD/D
(deptno SMALLINT Default is ON COMMIT DELETE ROWS
,avgsal DEC(9,2)
,maxsal DEC(9,2)
,minsal DEC(9,2)
,sumsal DEC(9,2)
,empcnt SMALLINT);
• Example: Create a report entitled “Autumn sale” that shows spring items marked 33% off and
summer items marked 25% off.
SELECT
item_number
,item_description
,item_price AS “Current//Price”
,CASE item_season
WHEN 'spring' THEN item_price * (1 - .33)
WHEN 'summer' THEN item_price * (1 - .25)
ELSE NULL
END AS "Sale//Price"
FROM Inventory_Table;
CASE Feature (cont.)
In a Searched CASE statement, you do not specify an expression to test. You specify multiple,
arbitrary, search conditions that can return different results.
Example: Repeat the previous page query, and mark down by 50% the summer items with inventories
of less than 3.
SELECT
item_number
,item_description
,item_price AS “Current//Price”
,CASE
WHEN item_season = 'summer' AND item_count < 3 THEN item_price * (1 - .50)
WHEN item_season = 'summer' THEN item_price * (1 - .25)
WHEN item_season = 'spring' THEN item_price * (1 - .33)
ELSE NULL
END AS "Sale//Price"
FROM Inventory_Table
WHERE item_season IN ('spring' OR 'summer');
Example of CASE – Single Pass Over Data
Four separate SQL statements can be used to generate the 4 totals. Instead of four
passes over the data using the WHERE clause, execute one pass using CASE.
SELECT
SUM (CASE WHEN EXTRACT (YEAR FROM sales_dt) = EXTRACT (YEAR FROM Current_Date)
THEN Sales
ELSE 0
END) AS YTD_Qty
,SUM (CASE WHEN EXTRACT (YEAR FROM sales_dt) = EXTRACT (YEAR FROM Current_Date)
AND EXTRACT (MONTH FROM sales_dt) = EXTRACT (MONTH FROM Current_Date)
THEN Sales
ELSE 0
END) AS MTD_Qty
,SUM (CASE WHEN EXTRACT (YEAR FROM sales_dt) = (EXTRACT (YEAR FROM Current_Date) - 1)
AND EXTRACT (MONTH FROM sales_dt) LE EXTRACT (MONTH FROM Current_Date)
AND EXTRACT (DAY FROM sales_dt) LE EXTRACT (DAY FROM Current_Date)
THEN Sales
ELSE 0
END) AS LastYear_YTD_Qty
,SUM (CASE WHEN EXTRACT (YEAR FROM sales_dt) = (EXTRACT (YEAR FROM Current_Date) - 1)
AND EXTRACT (MONTH FROM sales_dt) = EXTRACT (MONTH FROM Current_Date)
AND EXTRACT(DAY FROM sales_dt) LE EXTRACT (DAY FROM Current_Date)
THEN Sales
ELSE 0
END) AS LastYear_MTD_Qty
The Output:
FROM Sales_History;
YTD_Qty MTD_Qty LastYear_YTD_Qty LastYear_MTD_Qty
404 212 250 145
V2R5 – New Features
3. True or False. The SQL Flagger is just a warning device and doesn’t affect command execution
4. True or False. Failure of an individual request in COMMIT mode causes the entire transaction to be
rolled back.
5. True or False. Logging off during an explicit transaction without either a COMMIT or ET will always
result in a ROLLBACK.
6. True or False. HELP SESSION will show the session mode and the status of the SQL Flagger.
7. Where does a Volatile Temporary table get its space from? _____________
8. Where does a Global Temporary table get its space from? _____________
Module 18: Review Question Answers
1. Which BTEQ setting controls Teradata Vs. ANSI mode? .SET SESSION TRANSACTION
3. True or False. The SQL Flagger is just a warning device and doesn’t affect command execution.
4. True or False. Failure of an individual request in COMMIT mode causes the entire transaction to be
rolled back.
5. True or False. Logging off during an explicit transaction without either a COMMIT or ET will always
result in a ROLLBACK.
6. True or False. HELP SESSION will show the session mode and the status of the SQL Flagger.
7. Where does a Volatile Temporary table get its space from? Spool
8. Where does a Global Temporary table get its space from? Temporary