简体   繁体   中英

Column name or number of supplied values does not match table definition. But names and number of supplied values are correct

I have the following problem. I have a stored procedure which looks like the following:

truncate table  snap.MachineRegisteredSoftware
insert into snap.MachineRegisteredSoftware
select  IdentifyingKey,
        MachineID,
        Computername,
        FQDN,
        GETDATE(),
        GETDATE(),
        '9999-01-01 00:00:00.000',
        eDirGlobalID as GlobalID,
        PrimaryUser as PrimaryUsername,
        getDate(),
        cInstallDate as InstallDate
from exampleDB.ext.exampleView

Im trying to insert the above listed values in the following table:

在此处输入图片说明

When i try to execute the stored procedure i always get the error message

Msg 213, Level 16, State 1, Procedure Daily, Line 63 [Batch Start Line 7] Column name or number of supplied values does not match table definition.

I have no idea where the problem is. I would appreciate any kind of help.

Thanks in advance

EDIT: new stored procedure looks like this. still getting the same error.

    --Machine Registered Software
truncate table  snap.MachineRegisteredSoftware
insert into snap.MachineRegisteredSoftware (
    IdentifyingKey,
    MachineID,
    Computername,
    FQDN,
    GlobalID,
    LastScanDate,
    EffectiveStartDate,
    EffectiveEndDate,
    PrimaryUsername,
    CreatedOn,
    InstallDate
)
select  IdentifyingKey = IdentifyingKey,
        MachineID = MachineID,
        Computername = Computername,
        FQDN = FQDN,
        GlobalID = eDirGlobalID,
        LastScanDate = GETDATE(),
        EffectiveStartDate = GETDATE(),
        EffectiveEndDate = '9999-01-01 00:00:00.000',
        PrimaryUsername = PrimaryUser,
        CreatedOn = getdate(),
        InstallDate = cInstallDate
from exampleDB.ext.exampleView

EDIT2: I also do not have an Identity Column in the table. Also no triggers linked to the table.

Always write explicit columns on each insert:

insert into snap.MachineRegisteredSoftware (
    IdentifyingKey,
    MachineID,
    Computername,
    FWDN,
    GlobalID,
    LastScanDate,
    EffectiveStartDate,
    EffectiveEndDate,
    PrimaryUsername,
    CreatedOn,
    InstallDate)
select  
    IdentifyingKey,
    MachineID,
    Computername,
    FQDN,
    GETDATE(),
    GETDATE(),
    '9999-01-01 00:00:00.000',
    eDirGlobalID as GlobalID,
    PrimaryUser as PrimaryUsername,
    getDate(),
    cInstallDate as InstallDate
from 
    exampleDB.ext.exampleView

I also recommend forcing the alias of the SELECT to the same order.

insert into snap.MachineRegisteredSoftware (
    IdentifyingKey,
    MachineID,
    Computername,
    FQDN,
    GlobalID,
    LastScanDate,
    EffectiveStartDate,
    EffectiveEndDate,
    PrimaryUsername,
    CreatedOn,
    InstallDate)
select  
    IdentifyingKey = IdentifyingKey,
    MachineID = MachineID,
    Computername = Computername,
    FQDN = FQDN,
    GlobalID = GETDATE(),
    LastScanDate = GETDATE(),
    EffectiveStartDate = '9999-01-01 00:00:00.000',
    EffectiveEndDate = eDirGlobalID, --as GlobalID
    PrimaryUsername = PrimaryUser, --as PrimaryUsername
    CreatedOn = getDate(),
    InstallDate = cInstallDate --as InstallDate
from 
    exampleDB.ext.exampleView

You can clearly see the problem is on EffectiveEndDate here (and probably other columns too).

You might also have an IDENTITY column which doesn't need to be specified and needs to be removed from the list, or columns with DEFAULT constraints that you either want to omit or write explicitly the DEFAULT keyword.

Also make sure that the problem isn't in a trigger that's linked to the table you are inserting the data into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM