插入问题消息213,级别16,状态1,行2

时间:2014-03-27 15:37:22

标签: sql-server-2012

一般新手SQL问题probs ...

为什么这不起作用 - 或者,这是什么意思?

INSERT INTO test_db.dbo.Customers2
  SELECT FirstName
  FROM jas_test_db.dbo.Customers
  WHERE (Customers.FirstName = Firstname)

我明白了:

  

Msg 213,Level 16,State 1,Line 2
  列名或提供的值数与表定义不匹配。

2 个答案:

答案 0 :(得分:2)

您应该为Customers2提供列名:

INSERT INTO test_db.dbo.Customers2(**Firstname**)

SELECT FirstName

FROM jas_test_db.dbo.Customers

WHERE (Customers.FirstName = Firstname)

答案 1 :(得分:1)

我建议始终明确指定要插入的列:

INSERT INTO test_db.dbo.Customers2(FirstName)  -- <-- specify the columns you want to insert into!
  SELECT FirstName
  FROM jas_test_db.dbo.Customers
  WHERE (Customers.FirstName = Firstname)

如果您不这样做,则必须按照所有列的值,按照表中定义的确切顺序提供