【问题标题】:Column name or number of supplied values doesn't match table definition sql [closed]列名或提供的值的数量与表定义 sql 不匹配 [关闭]
【发布时间】:2020-04-13 07:45:10
【问题描述】:

在 SQL Server 中,我尝试使用以下查询在表中插入值:

ALTER TABLE student ADD NrLeg_sot varchar(5)

INSERT INTO student 
VALUES ('117', 'Popescu', 'F', 'Florentina', 'f', CONVERT (smalldatetime, '15/04/1978', 103), 'C', '223', '102')

我有这张桌子:

DROP TABLE student;

CREATE TABLE student 
(
    NrLeg int NOT NULL PRIMARY KEY,
    Name varchar(20),
    Initiala varchar(5),
    Prenume varchar(20),
    Grupa int,
    Nota int,
    CodDisciplina int ,
    gen varchar(5),
    DataS  datetime
); 

我收到以下错误:

列名或提供的值的数量与表定义不匹配。

为什么?

【问题讨论】:

  • 您的alter tableinsert 之间需要一个GO 才能使更改生效。
  • @DaleK 该错误与GO 无关,并且与GO 无关。

标签: sql sql-server tsql sql-server-2008


【解决方案1】:

你有以下陈述:

INSERT INTO student VALUES(
'117',     -- maps to NrLeg which has data type int
'Popescu', -- maps to Name
'F',       -- maps to Initiala
'Florentina', -- maps to Prenume
'f',       -- maps to Grupa, which is of data type int
CONVERT (smalldatetime,'15/04/1978',103), -- maps to Nota, also an int
'C',       -- maps to CodDiscipline, an int
'223',     -- maps to gen
'102'      -- maps to DataS, a datetime
)

IOW 在您的插入语句中没有多少可以工作的东西。 但是,如果您要添加列名列表(并调整数据类型),数据库引擎就知道如何将指定的值映射到列:

INSERT INTO student (NrLeg, Name, Initiala, Prenume, gen, DataS, Grupa, Nota,
                     CodDiscipline)
VALUES(
117,       -- maps to NrLeg which has data type int
'Popescu', -- maps to Name
'F',       -- maps to Initiala
'Florentina', -- maps to Prenume
'f',       -- maps to gen
CONVERT (smalldatetime,'15/04/1978',103), -- maps to DataS
'C',       -- maps to Grupa, an int, this is still a problem
223,       -- maps to Nota
102        -- maps to CodDiscipline
)

不过,值“C”仍然存在问题。

【讨论】:

    【解决方案2】:

    您可以匹配以下查询。您应该传递不带单引号的整数列值。

    此外,当您以以下格式编写查询时,可以轻松验证列计数的顺序和数量。

    create table student (
        NrLeg int NOT NULL PRIMARY KEY,
        Name varchar(20),
        Initiala varchar(5),
        Prenume varchar(20),
        Grupa int,
        Nota int,
        CodDisciplina int ,
        gen varchar(5),
        DataS  datetime
    ); 
    
    ALTER TABLE student ADD NrLeg_sot varchar(5)
    
    INSERT INTO student 
    VALUES(117 --NrLeg
    ,'Popescu' --Name
    ,'F' --Initiala 
    ,'Florentina' --Prenume
    , 0 --Grupa
    , 0 --Nota
    , 0 --CodDisciplina
    ,'C' --gen
    ,CONVERT (smalldatetime,'15/04/1978',103) --DataS
    ,'102' --NrLeg_sot
    )
    
    Select * from student
    

    这是现场db<>fiddle 演示。

    如下所示,在插入表格时将列名放在括号中是一种很好的做法。

    INSERT INTO student (<column1 name>, <column2 name>, <column3 name>, ...)
    Values(<column1 value>, <column2 value>, <column3 value>, ...)
    

    【讨论】:

    • 仅供参考:您可以在 SQL Server 中传递带引号的整数。
    • @DaleK 该想法已根据您的第二个建议进行了修改和编辑。在 OP 情况下还有其他问题,这只是建议不要使用单引号来传递整数值。
    猜你喜欢
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 2010-11-12
    • 2021-11-06
    • 2020-01-29
    相关资源
    最近更新 更多