为什么我在SQL Server中出现此错误?

时间:2015-08-08 07:38:10

标签: sql-server

我写这个查询:

INSERT INTO Customer_Care_Database_Analysis_Center
   SELECT code_markaz, name_markaz  
   FROM Customer_Care_Database_Analysis_TEMP
   WHERE NOT EXISTS (SELECT 1
                     FROM Customer_Care_Database_Analysis_TEMP ti
                     WHERE ti.code_markaz = Customer_Care_Database_Analysis_Center.code_markaz);

但是当我运行该查询时出现此错误:

  

Msg 4104,Level 16,State 1,Line 6
  多部分标识符" Customer_Care_Database_Analysis_Center.code_markaz"无法受约束。

发生什么事了?我该如何解决?

3 个答案:

答案 0 :(得分:1)

不确定答案会发生什么,但我会假设您要插入表格中不存在的行(字段代码_markaz),并且您可以这样做:

INSERT INTO Customer_Care_Database_Analysis_Center (code_markaz, name_markaz)
SELECT code_markaz, name_markaz  
FROM Customer_Care_Database_Analysis_TEMP tmp
WHERE NOT EXISTS (SELECT 1
      FROM Customer_Care_Database_Analysis_Center c
      WHERE c.code_markaz = tmp.code_markaz)

我还在insert语句中添加了列。如果表格中只有2列,则无需使用它们。

答案 1 :(得分:-1)

插入(Customer_Care_Database_Analysis_Center)的表没有名为code_markaz的列。

命名目标列:

INSERT INTO Customer_Care_Database_Analysis_Center (some_col_1, some_col_2)
   SELECT code_markaz, name_markaz  
   FROM ...

答案 2 :(得分:-1)

错误是由于Customer_Care_Database_Analysis_Center表没有引用,因为你不存在内部查询。

如上所述,您必须提供别名或加入该表。

...FROM Customer_Care_Database_Analysis_TEMP t
   JOIN Customer_Care_Database_Analysis_Center ccdac
    NOT EXISTS (
        SELECT 1
        FROM Customer_Care_Database_Analysis_TEMP ti
        WHERE ti.code_markaz = CCDAC.code_markaz);