简体   繁体   中英

SQL Syntax: SQL Server vs. Teradata

I've been querying against Teradata servers with SQL Assistant for years, but now have to work with a SQL Server. I've been stumbling over my code for hours, having a hard time figuring out which pieces of syntax need to be updated.

Does anyone know of a good resource for converting logic?

Here's an example -- I was loading .txt data into a temp table:

In Teradata, the following works:

CREATE MULTISET TABLE USER_WORK.TABLE1 (
VAR1 CHAR(3)
,VAR2 CHAR(5)
,VAR3 DECIMAL(12,2)  )
PRIMARY INDEX (VAR1, VAR2);

In SQL Server, I was able to get the following to work:

CREATE TABLE #TABLE1 (
VAR1 VARCHAR(20)
,VAR2 VARCHAR(20)
,VAR3 VAR(20)  );

(Main differences: No "Multiset"; all variables read in as VARCHAR & and I couldn't get any length shorter than 20 to work; I couldn't figure out how to define a functional index)

Mostly wondering if there is some sort of pattern behind migrating the logic - it's painful to have to look up every single piece of failed code, and to sort out of it will actually run on SQL Server.

A few points...

  • The # prefix in your SQL Server attempt defines a local temporary table. It's visible to your session only, and it will go away when the session ends. I think it's similar to a VOLATILE table in Teradata. Is that what you wanted?

  • SQL Server tables are MULTISET by default so SQL has no equivalent keyword.

  • If you were having trouble with CHAR column sizes it was most likely a syntax error elsewhere. CHAR columns can be from 1 to 8,000 characters long, using a single-byte character set.

  • SQL Server doesn't have a PRIMARY INDEX . As I understand it, the equivalent in SQL Server is a CLUSTERED index .

So your exact table structure in SQL Server would be like this:

CREATE TABLE USER_WORK.TABLE1 (
  VAR1 CHAR(3)
  ,VAR2 CHAR(5)
  ,VAR3 DECIMAL(12,2));

And for the index (the name can be whatever you want):

CREATE CLUSTERED INDEX TABLE1_FOO ON USER_WORK.TABLE1(VAR1, VAR2);

You can create the exact same schema in sql server as well but the syntax will be a bit different.

I would translate your teradata table as below:

CREATE TABLE TABLE1 
 ( VAR1   CHAR(3)       NOT NULL
  ,VAR2   CHAR(5)       NOT NULL
  ,VAR3   DECIMAL(12,2)  
  ,PRIMARY KEY (VAR1, VAR2)
);
GO

You can still have CHAR(3) and CHAR(5) data types for VAR1 and VAR2 columns, but you have to make them non-nullable column since they are going to be Primary key columns ( requirement in sql server).

Sql server also has data type decimal(12,2) you can use it for your VAR3 column. finally the composite primary key can be part of the table definition as shows above. tar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM