Insert issue Msg 213, Level 16, State 1, Line 2

牧云@^-^@ 提交于 2019-12-23 05:16:05

问题


General newbie SQL question probs...

Why doesn't this work - or, what does this mean?

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

I get:

Msg 213, Level 16, State 1, Line 2
Column name or number of supplied values does not match table definition.


回答1:


You should supply column name for Customers2:

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

SELECT FirstName

FROM jas_test_db.dbo.Customers

WHERE (Customers.FirstName = Firstname)



回答2:


I would recommend to always explicitly specify which columns you want to insert into:

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)

If you don't do this, you have to provide values for all columns, in the exact order in which they are defined in the table



来源:https://stackoverflow.com/questions/22692399/insert-issue-msg-213-level-16-state-1-line-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!