The SQL Server DBA’s Guide to Teradata
Being a long-time devotee of all things Microsoft SQL Server, I had quite a bit of learning to do to get up to speed on our new Teradata appliance. This guide attempts to distill some of what I have learned. I will primarily focus on what I have found most lacking: examples of how to convert SQL Server T-SQL to Teradata SQL. This guide will use the vendor-supplied sample databases: SQL Server’s AdventureWorksDW2012, and the Teradata’s Express 14.0 Retail database.
Also, while this is probably self explanatory, the “mufford” object referenced in the Teradata examples is simply my own personal sandbox. You’ll need to replace any “mufford” reference to whatever your development sandbox may be named.
Release History:
- 2012-08-17 – Added UPDATE examples; reformatted
- 2012-08-12 – Added date examples
- 2012-08-08 – Initial Release
SELECT Statements
Simple SELECT
SQL Server:
Best practice is to qualify the table schema; qualifying the database is optional unless you plan to perform cross-database queries.
Best practice is to qualify the table schema; qualifying the database is optional unless you plan to perform cross-database queries.
Teradata:
Teradata does not support the concept of schemas, so you would select directly from a database and table.
Teradata does not support the concept of schemas, so you would select directly from a database and table.
UPDATE Statements
Update From a Subquery
SQL Server:
To help demonstrate a more complex UPDATE, I am going to create a temporary table, insert into the temporary table, and then update that same table. Please don’t get caught up in the data so much as the overall syntax.
To help demonstrate a more complex UPDATE, I am going to create a temporary table, insert into the temporary table, and then update that same table. Please don’t get caught up in the data so much as the overall syntax.
Teradata:
We’re going to do the same thing in Teradata: create a temporary (volatile) table, populate it, then perform an update. However, there are 2 different ways to write the UPDATE statement. Both are shown here.
We’re going to do the same thing in Teradata: create a temporary (volatile) table, populate it, then perform an update. However, there are 2 different ways to write the UPDATE statement. Both are shown here.
DELETE Statements
Delete Top X
SQL Server:
This is pretty straight-forward in SQL Server.
This is pretty straight-forward in SQL Server.
Teradata:
Because Teradata is intended to be a data warehouse, not an OLTP system, this is a little more involved. We need to first create a temporary table that contains the rows we wish to remove, then perform the DELETE using the IN clause.
Because Teradata is intended to be a data warehouse, not an OLTP system, this is a little more involved. We need to first create a temporary table that contains the rows we wish to remove, then perform the DELETE using the IN clause.
Delete where a relationship exists
SQL Server:
Teradata:
Again, you can do this by using a subquery.
Again, you can do this by using a subquery.
Delete where a relationship does not exist:
SQL Server:
Teradata:
Once more, we’ll use a subquery to perform our delete.
Once more, we’ll use a subquery to perform our delete.
Temporary / Volatile Tables
Creating a simple temporary table
SQL Server:
The simple way:
The simple way:
The verbose, “best practice” way:
Teradata:
Teradata’s equivalent of temporary tables is volatile tables.
Teradata’s equivalent of temporary tables is volatile tables.
Using Dates
Find Within Last X Days
SQL Server:
Teradata:
Calculate the Number of Days Between 2 Dates
SQL Server:
Teradata:
Using dates in variables
SQL Server:
Teradata:
We’ll use a macro instead of a stored procedure for returning this same type of data in Teradata.
We’ll use a macro instead of a stored procedure for returning this same type of data in Teradata.
Note: if you get a “An owner referenced by user does not have SELECT WITH GRANT OPTION” error when you try to execute this, make sure you run this:
Performing date calculations using variables
SQL Server:
Teradata:
We’ll use a macro instead of a stored procedure for returning this same type of data in Teradata.
We’ll use a macro instead of a stored procedure for returning this same type of data in Teradata.
Teradata:
Miscellaneous
NULL Handling
SQL Server:
Teradata:
Comments
Post a Comment