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

Posted

技术标签:

【中文标题】SQL 列名或提供的值的数量与表定义不匹配【英文标题】:SQL Column name or number of supplied values does not match table definition 【发布时间】:2014-05-27 16:05:39 【问题描述】:

当我尝试运行我的存储过程时遇到了这个错误,我检查了插入是否与选择匹配,这似乎是大多数时候问这个问题的问题。据我所知,它们匹配,所以其他东西一定是错的。

提前致谢

错误:

消息 213,级别 16,状态 1,过程 system_IncreaseCustomTariffs,第 36 行 列名或提供的值的数量与表定义不匹配。

代码:

--This sp is used by doRateIncrease.exe
ALTER PROCEDURE [dbo].[system_IncreaseCustomTariffs]
            @Increase money,
            @ProgramType varchar(30),
            @StartDate varchar(10) = NULL,
            @StopDate varchar(10) = NULL,
            @Exclude varchar(1024) = ''
AS  
   SET NOCOUNT ON

   DECLARE @OldFreightId int, @CustFreightId int, @BillAcct int, @User varchar(30)

   SET @User = 'Increase_' + Replace(Convert(varchar(10), GetDate(), 101),'/', '-')

   --Delete existing tariffs
   DECLARE C1 CURSOR FOR 
        SELECT CustFreightId 
        FROM CustFreightProgramMaster 
        WHERE ProgramType = @ProgramType and CreateUsr = @User

   OPEN C1
   FETCH NEXT FROM C1 INTO @CustFreightId

   WHILE (@@FETCH_STATUS = 0)  
   BEGIN
       EXECUTE delete_CustFreightProgramMaster @CustFreightId = @CustFreightId

       FETCH NEXT FROM C1 INTO @CustFreightId
   END

   CLOSE C1
   DEALLOCATE C1

   --Load exclusion table
   CREATE TABLE #Exclude (parseValue VARCHAR(100))

   INSERT INTO #Exclude
       SELECT * 
       FROM dbo.Parse (@Exclude, ',')
       WHERE IsNull(parseValue,'') <> ''

--Get all active tariffs
DECLARE Increase CURSOR FOR 
    SELECT CustFreightId, BillAcct FROM CustFreightProgramMaster
        WHERE ProgramType = @ProgramType
          and Convert(varchar(10), GetDate(), 101) Between StartDate and StopDate

OPEN Increase
FETCH NEXT FROM Increase INTO @OldFreightId, @BillAcct
WHILE (@@FETCH_STATUS = 0)  
BEGIN
    --If BillAcct excluded from increase just extend stop date
    IF EXISTS (SELECT * FROM #Exclude WHERE Cast(parseValue as int)= @BillAcct)
    BEGIN
        --Extend existing tariff to new stop date
        UPDATE CustFreightProgramMaster
            SET StopDate = @StopDate
            WHERE CustFreightId = @OldFreightId
    END

    --Create new tariff
    ELSE
    BEGIN
        --Expire existing tariff 1 day prior to start on new tariff
        UPDATE CustFreightProgramMaster
            SET StopDate = DateAdd(day, -1, @StartDate)
            WHERE CustFreightId = @OldFreightId

        --Create new program master
        INSERT INTO CustFreightProgramMaster(BillAcct, ProgramType, BiDirect, StartDate, StopDate, 
                AbsoluteMinimum, MaxDiscount, MaxPalletWeight, CreateSvr, CreateTrm, CreateUsr, CreateDT)
            SELECT BillAcct, ProgramType, BiDirect, @StartDate, @StopDate, AbsoluteMinimum, 
                MaxDiscount, MaxPalletWeight, CreateSvr, CreateTrm, @User, GetDate()
            FROM CustFreightProgramMaster WHERE CustFreightId = @OldFreightId


        --Get new FreightId
        SET @CustFreightId = 0
        SET @CustFreightId = SCOPE_IDENTITY()

        IF IsNull(@CustFreightId,0) = 0
        BEGIN
            RAISERROR('Tariff not created.',16,1)
            RETURN
        END

        --Copy Origin Zones
        INSERT INTO CustFreightProgramOriginZones (CustFreightId, ZoneId, CreateSvr, CreateTrm, CreateUsr, CreateDT)
            SELECT @CustFreightId, ZoneId, CreateSvr, CreateTrm, @User, GetDate() 
            FROM CustFreightProgramOriginZones WHERE CustFreightId = @OldFreightId

        --Copy Destination Zones
        INSERT INTO CustFreightProgramDestZones (CustFreightId, ZoneId, CreateSvr, CreateTrm, CreateUsr, CreateDT)
            SELECT @CustFreightId, ZoneId, CreateSvr, CreateTrm, @User, GetDate() 
            FROM CustFreightProgramDestZones WHERE CustFreightId = @OldFreightId

        --Update Customer Program Master program description
        UPDATE CustFreightProgramMaster
            SET ProgramDesc = dbo.BuildCustFreightProgramDesc(CustFreightId)
            WHERE CustFreightId = @CustFreightId

        --Copy weight breaks, apply increase
        INSERT INTO CustFreightProgramWeightBreaks (CustFreightId, LowValue, HighValue, UnitCost, CreateSvr, CreateTrm, CreateUsr, CreateDT)
            SELECT @CustFreightId, LowValue, HighValue, dbo.IncreaseUnitCost(@ProgramType, UnitCost, @Increase), CreateSvr, CreateTrm, @User, GetDate() 
            FROM CustFreightProgramWeightBreaks WHERE CustFreightId = @OldFreightId

        --Copy pallet breaks, apply increase
        INSERT INTO CustFreightProgramPalletRates (CustFreightId, LowValue, HighValue, UnitCost, CreateSvr, CreateTrm, CreateUsr, CreateDT)
                SELECT @CustFreightId, LowValue, HighValue, dbo.IncreaseUnitCost(@ProgramType, UnitCost, @Increase), CreateSvr, CreateTrm, @User, GetDate() 
            FROM CustFreightProgramPalletRates WHERE CustFreightId = @OldFreightId
    END

    FETCH NEXT FROM Increase INTO @OldFreightId, @BillAcct
END

CLOSE Increase
DEALLOCATE Increase 

--Clean up
DROP TABLE #Exclude

【问题讨论】:

【参考方案1】:

看来问题出在这里:

INSERT INTO #Exclude
    SELECT * FROM dbo.Parse (@Exclude, ',')
        WHERE IsNull(parseValue,'') <> ''

也许宁愿做到:

INSERT INTO #Exclude
    SELECT [Value - Or name of first column] FROM dbo.Parse (@Exclude, ',')
        WHERE IsNull(parseValue,'') <> ''

【讨论】:

就是这样,将 * 更改为 parseValue 并且成功了,感谢您的快速响应

以上是关于SQL 列名或提供的值的数量与表定义不匹配的主要内容,如果未能解决你的问题,请参考以下文章

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

插入查询:列名或提供的值的数量与表定义不匹配

请任何人清除错误:列名或提供的值的数量与表定义不匹配

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

获取错误列名称或提供的值的数量与表定义不匹配

sql server 2005代码错误 提示插入错误: 列名或所提供值的数目与表定义不匹配。