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

前端 未结 12 1764
轮回少年
轮回少年 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:01

    For me the culprit is int value assigned to salary

    Insert into Employees(ID,FirstName,LastName,Gender,Salary) values(3,'Canada', 'pa', 'm',15,000)

    in salary column When we assign 15,000 the compiler understand 15 and 000.

    This correction works fine for me. Insert into Employees(ID,FirstName,LastName,Gender,Salary) values(4,'US', 'sam', 'm',15000)

    0 讨论(0)
  • 2020-11-29 05:02

    The problem I had that caused this error was that I was trying to insert null values into a NOT NULL column.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-29 05:07

    The computed columns make the problem. Do not use SELECT *. You must specify each fields after SELECT except computed fields

    0 讨论(0)
  • 2020-11-29 05:12

    I hope you have found a good solution. I had the same problem, and the way I worked around it is probably not the best but it is working now.

    it involves creating a linked server and using dynamic sql - not the best, but if anyone can suggest something better, please comment/answer.

    declare @sql nvarchar(max)
    
    
    DECLARE @DB_SPACE TABLE (
    [DatabaseName] NVARCHAR(128) NOT NULL,
    [FILEID] [smallint] NOT NULL,
    [FILE_SIZE_MB] INT NOT NULL DEFAULT (0),
    [SPACE_USED_MB] INT NULL DEFAULT (0),
    [FREE_SPACE_MB] INT NULL DEFAULT (0),
    [LOGICALNAME] SYSNAME NOT NULL,
    [DRIVE] NCHAR(1) NOT NULL,
    [FILENAME] NVARCHAR(260) NOT NULL,
    [FILE_TYPE] NVARCHAR(260) NOT NULL,
    [THE_AUTOGROWTH_IN_KB] INT NOT NULL DEFAULT(0)
    ,filegroup VARCHAR(128)
    ,maxsize VARCHAR(25)
    
    PRIMARY KEY CLUSTERED ([DatabaseName] ,[FILEID] )
    )  
    
    
    SELECT @SQL ='SELECT [DatabaseName],
            [FILEID],
            [FILE_SIZE_MB],
            [SPACE_USED_MB],
            [FREE_SPACE_MB],
            [LOGICALNAME],
            [DRIVE],
            [FILENAME],
            [FILE_TYPE],
            [THE_AUTOGROWTH_IN_KB]
            ,filegroup
            ,maxsize FROM OPENQUERY('+ QUOTENAME('THE_MONITOR') + ','''+ ' EXEC MASTER.DBO.monitoring_database_details '  +''')'
    exec sp_executesql @sql
    
    
          INSERT INTO @DB_SPACE(
                                [DatabaseName],
                                [FILEID],
                                [FILE_SIZE_MB],
                                [SPACE_USED_MB],
                                [FREE_SPACE_MB],
                                [LOGICALNAME],
                                [DRIVE],
                                [FILENAME],
                                [FILE_TYPE],
                                THE_AUTOGROWTH_IN_KB,
                                [filegroup],
                                maxsize
                              )
    
          EXEC SP_EXECUTESQL @SQL
    

    this is working for me now. I can guarantee the number of columns and type of columns returned by the stored procedure are the same as in this table, simply because I return the same table from the stored procedure.

    thanks and regards marcelo

    0 讨论(0)
  • 2020-11-29 05:12

    Check your id. Is it Identity? If it is then make sure it is declared as ID not null Identity(1,1)

    And before creating your table , Drop table and then create table.

    0 讨论(0)
提交回复
热议问题