简体   繁体   English

我的T-SQL语法有什么问题?

[英]What is wrong with my T-SQL syntax?

I keep getting the following error message when I try to run this statement. 当我尝试运行此语句时,我不断收到以下错误消息。 I'm running it inside of an OLE DB command in SSIS. 我在SSIS中的OLE DB命令中运行它。

Incorrect syntax near ')' ')'附近的语法不正确

Obviously this is an easy to read error message but I don't know why I'm getting it. 显然,这是一个易于阅读的错误消息,但我不知道为什么要得到它。 I've checked the T-SQL documentation and I'm pretty sure I have parenthesis where they are needed. 我检查了T-SQL文档,并确定在需要的地方加上括号。

INSERT INTO
  dbo.Table1 (
    [ID],
    [Supplier],
    [Level],
    [Status],
    [Core],
    [Location],
    [Outsourced],
    [Contact],
    [Phone],
    [Email]
  )
SELECT
  [ID],
  [Supplier],
  [Level],
  [Status],
  [Core],
  [Location],
  [Outsourced],
  [Contact],
  [Phone],
  [Email]
FROM
  dbo.Table2
WHERE
  NOT (
    [ID],
    [Supplier],
    [Level],
    [Status],
    [Core],
    [Location],
    [Outsourced],
    [Contact],
    [Phone],
    [Email] IN (
      SELECT
        [ID],
        [Supplier],
        [Level],
        [Status],
        [Core],
        [Location],
        [Outsourced],
        [Contact],
        [Phone],
        [Email]
      FROM
        dbo.table1
    )
  )

Update 更新资料

Martin Smith wrote a great comment on M.Ali's answer - Are you sure you are doing the right thing here? 马丁·史密斯(Martin Smith)对马里·阿里(A.Ali)的回答写了一个很好的评论 -您确定在这里做对了吗? If any of the values in any of the columns is different in both tables, the select statement will return it. 如果两个表中任何列中的任何值都不相同,则select语句将返回它。
This means that if you have a record in table 2 that has the all the same values except one with a record in table 1, the select statement will return it and you might get either a primary key violation error of worst - duplicated rows (except that single column) in you table 1. 这意味着,如果表2中有一条记录具有相同的值,但表1中有一条记录,则select语句将返回它,并且您可能会遇到最坏的主键冲突错误-重复行(除非表1中的单个列)。
Taking that into consideration what you should probably do is select all the records from table 2 where the key columns does not exists in table 1 - any column that is a part of any unique constraint or index, not just the primary key. 考虑到这一点,您可能应该做的是从表2中选择键1在表1中不存在的所有记录,表2是任何唯一约束或索引的一部分,而不仅仅是主键。

So, assuming ID is the primary key, and for the sake of the demonstration you have a unique index on Phone and Email , what you need is something like this: 因此,假设ID是主键,并且为了进行演示,您在PhoneEmail上具有唯一索引,那么您需要的是这样的:

INSERT INTO
  dbo.Table1 (
    [ID],
    [Supplier],
    [Level],
    [Status],
    [Core],
    [Location],
    [Outsourced],
    [Contact],
    [Phone],
    [Email]
  )
SELECT
  [ID],
  [Supplier],
  [Level],
  [Status],
  [Core],
  [Location],
  [Outsourced],
  [Contact],
  [Phone],
  [Email]
FROM
  dbo.Table2 t2
WHERE NOT EXISTS
(
    SELECT 1
    FROM Table1 t1
    WHERE t1.ID = t2.ID
    OR (t1.Phone = t2.Phone AND t1.Email = t2.Email)
)

First version 第一版

The IN operator doesn't work like that in SQL Server. IN运算符不能像在SQL Server中那样工作。 It can have only one operand on it's left side, and many operands on it's rights side, so it basically compares a single value to a list. 它的左侧只能有一个操作数,而右侧则可以有许多操作数,因此它基本上将单个值与列表进行比较。 T-SQL IN operator works like this: T-SQL IN运算符的工作方式如下:

Col IN(value1, value2....valuen)

is equivalent to 相当于

Col = value1 or Col = value2....or Col = valuen`   

It is valid in standard SQL and there are databases that supports it such as MySql, but SQL Server doesn't. 它在标准SQL中有效,并且有支持它的数据库(例如MySql),但SQL Server不支持。

You are looking for EXCEPT : 您正在寻找EXCEPT

INSERT INTO
  dbo.Table1 (
    [ID],
    [Supplier],
    [Level],
    [Status],
    [Core],
    [Location],
    [Outsourced],
    [Contact],
    [Phone],
    [Email]
  )
SELECT
  [ID],
  [Supplier],
  [Level],
  [Status],
  [Core],
  [Location],
  [Outsourced],
  [Contact],
  [Phone],
  [Email]
FROM
  dbo.Table2
EXCEPT 
      SELECT
        [ID],
        [Supplier],
        [Level],
        [Status],
        [Core],
        [Location],
        [Outsourced],
        [Contact],
        [Phone],
        [Email]
      FROM
        dbo.table1

Using Exists can give you a much faster query, sometimes even faster than a left join where righttable.PK_Column is null queries. 使用“ Exists可以给您更快的查询,有时甚至比left join where righttable.PK_Column is null查询)还要快。

INSERT INTO dbo.Table1 
          ([ID],[Supplier],[Level],[Status],[Core],[Location]
            ,[Outsourced],[Contact],[Phone],[Email])
SELECT
  [ID],
  [Supplier],
  [Level],
  [Status],
  [Core],
  [Location],
  [Outsourced],
  [Contact],
  [Phone],
  [Email]
FROM
  dbo.Table2 t2
WHERE NOT EXISTS ( SELECT 1 
                   FROM dbo.table1 t1
                    WHERE t2.[ID]        = t2.[ID]
                          t2.[Supplier]  = t2.[Supplier]
                          t2.[Level]     = t2.[Level]
                          t2.[Status]    = t2.[Status]
                          t2.[Core]      = t2.[Core]
                          t2.[Location]  = t2.[Location]
                          t2.[Outsourced]= t2.[Outsourced]
                          t2.[Contact]   = t2.[Contact]
                          t2.[Phone]     = t2.[Phone]
                          t2.[Email]     = t2.[Email] 
                  )

IN takes exactly ONE column name on the left, you can't put a list of columns on the left. IN的左侧恰好有一个列名,您不能在左侧放置一列列表。 I'm honestly not exactly sure what you're going for here... it doesn't make a lot of sense. 老实说,我不确定您要在这里做什么……这没有任何意义。 But you probably want something more like this: 但是您可能想要更多这样的东西:

WHERE
    [ID] NOT IN (SELECT [ID] FROM dbo.Table1)
AND [Supplier] NOT IN (SELECT [Supplier] FROM dbo.Table1)
AND ...

But maybe you mean something more like this: 但也许您的意思是这样的:

WHERE NOT EXISTS (SELECT 1 FROM dbo.Table1 WHERE Table1.ID = Table2.ID 
                                             AND Table1.Supplier = Table2.Supplier 
                                             AND ... )

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

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