Column name or number of supplied values does not match table definition

前端 未结 12 1808
轮回少年
轮回少年 2020-11-29 04:33

In SQL server, I am trying to insert values from one table to another by using the below query:

delete from tblTable1

insert into tblTable1 select * from tb         


        
12条回答
  •  离开以前
    2020-11-29 05:05

    for inserts it is always better to specify the column names see the following

    DECLARE @Table TABLE(
            Val1 VARCHAR(MAX)
    )
    
    INSERT INTO @Table SELECT '1'
    

    works fine, changing the table def to causes the error

    DECLARE @Table TABLE(
            Val1 VARCHAR(MAX),
            Val2 VARCHAR(MAX)
    )
    
    INSERT INTO @Table SELECT '1'
    

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

    But changing the above to

    DECLARE @Table TABLE(
            Val1 VARCHAR(MAX),
            Val2 VARCHAR(MAX)
    )
    
    INSERT INTO @Table (Val1)  SELECT '1'
    

    works. You need to be more specific with the columns specified

    supply the structures and we can have a look

提交回复
热议问题