简体   繁体   English

SQL语法:SQL Server与Teradata

[英]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. 多年来,我一直在使用SQL Assistant对Teradata服务器进行查询,但现在必须与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: 这是一个示例-我正在将.txt数据加载到临时表中:

In Teradata, the following works: 在Teradata中,以下工作原理:

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: 在SQL Server中,我可以执行以下操作:

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) (主要区别:没有“ Multiset”;所有变量都以VARCHAR&的形式读入,并且我无法获得小于20的任何长度来工作;我不知道如何定义函数索引)

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. 大多数人想知道逻辑迁移背后是否存在某种模式-必须查找每一个失败的代码片段,然后对其进行整理实际上会在SQL Server上运行,这很痛苦。

A few points... 几点...

  • The # prefix in your SQL Server attempt defines a local temporary table. SQL Server尝试中的#前缀定义了本地临时表。 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. 我认为它类似于Teradata中的VOLATILE表。 Is that what you wanted? 那是你想要的吗?

  • SQL Server tables are MULTISET by default so SQL has no equivalent keyword. 默认情况下,SQL Server表是MULTISET ,因此SQL没有等效的关键字。

  • If you were having trouble with CHAR column sizes it was most likely a syntax error elsewhere. 如果您在CHAR列大小方面遇到麻烦,则很可能是其他地方的语法错误。 CHAR columns can be from 1 to 8,000 characters long, using a single-byte character set. 使用单字节字符集, CHAR列的长度可以从1到8,000个字符。

  • SQL Server doesn't have a PRIMARY INDEX . SQL Server没有PRIMARY INDEX As I understand it, the equivalent in SQL Server is a CLUSTERED index . 据我了解,SQL Server中的等效项是CLUSTERED索引

So your exact table structure in SQL Server would be like this: 因此,您在SQL Server中的确切表结构将如下所示:

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. 您也可以在sql server中创建完全相同的架构,但是语法会有所不同。

I would translate your teradata table as below: 我将如下转换您的Teradata表:

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). 对于VAR1和VAR2列,您仍然可以具有CHAR(3)和CHAR(5)数据类型,但是必须将它们设置为非空列,因为它们将成为主键列(SQL Server中的要求)。

Sql server also has data type decimal(12,2) you can use it for your VAR3 column. SQL Server的数据类型为decimal(12,2),您可以将其用于VAR3列。 finally the composite primary key can be part of the table definition as shows above. 最后,复合主键可以成为表定义的一部分,如上所示。 tar 柏油

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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