【问题标题】:Stored Procedure Insert Error: Column name or number of supplied values does not match table definition存储过程插入错误:列名或提供的值的数量与表定义不匹配
【发布时间】:2014-01-22 00:19:57
【问题描述】:

这是我得到的唯一错误。请帮忙。提前谢谢你。

我在 SQL Server 中成功创建了一个存储过程后。我尝试使用以下(仅示例)执行它

exec [dbo].ExportResourceTime 'ABC111', '', 3, 1, '2013-10-01', '2013-10-30', 1, 21  

然后我收到以下错误。见附图。

这是 SSMS 中的实际错误。第 96 行是 if @OrgUnit ''

顶部的灰色光标

这是我认为错误来自的独立脚本。如果我错了,请纠正我。

消息 213,级别 16,状态 1,过程 ExportResourceTime,第 96 行
插入错误:列名或提供的值的数量与表定义不匹配。

代码:

create procedure [dbo].ExportResourceTime
        @ResourceID nvarchar(30),
            @OrgUnit nvarchar(15),
            @TimeDetail int,
            @ExpenseDetail int,
            @FromDate Datetime,
            @ToDate Datetime,
            @IncludeID int,
            @TimeTypeGroup int
            --1 = No Time Type Group
            --2 = Group by Time Type
as
BEGIN 
   create table #ItemisedTimeandMaterials
   (
      IDNo int,
      OrderBy1 varchar(60),
      ItemDate datetime,--MOD005
      RevenueTypeCode varchar(24),
      TimeType varchar(24),
      ProjectCode varchar(20),
      taskUID int,
      OutlineNum varchar(60),
      taskname varchar(60),
      activitycode varchar(24),
      ActivityDesc varchar(60),
      ResourceID nvarchar(24),
      OrganizationID nvarchar(15),
      EffectiveDate datetime,
      firstname varchar(60),
      lastname varchar(60),
      ExpenseTypeCode varchar(24),
      ExpenseTypeDesc varchar(60),
      Hours decimal(8,2),
      Rate decimal(8,2),
      Total decimal(20,8),
      Descr varchar(256), --MOD005 DM Added col for relevant detail for Expenses
      TimeTypeCode nvarchar(10)
     )
--GW: move this bit to the top--DONE

create table #Resources
(
ResourceID nvarchar(30),
OrganizationID nvarchar(15),
EffectiveDate datetime
)


if @ResourceID <> ''
 begin
 insert into #Resources (ResourceID,OrganizationID,EffectiveDate)

    select ro.ResourceID, ro.OrganizationID, ro.EffectiveDate from ResourceOrganization ro,
    (select ResourceID, MAX(EffectiveDate) as maxEffectivedate from dbo.ResourceOrganization
    **where ResourceID = @ResourceID**
    group by ResourceID) as maxresults
    where ro.ResourceID = maxresults.ResourceID
    and ro.EffectiveDate = maxresults.maxEffectivedate
    end

if @OrgUnit <> ''
 begin
 insert into #Resources (ResourceID,OrganizationID,EffectiveDate)
  Select  ResourceID,OrganizationID,EffectiveDate from ResourceOrganization
  where OrganizationID like '' + @OrgUnit + '%'
   end

-- get actual time - REGULAR
insert into #ItemisedTimeandMaterials
select      
Case when @IncludeID = 1 then b.timeID else '' end, --mod 07
e.lastname + e.firstname, 
case when @TimeDetail = 2 then g.enddate else (case when @TimeDetail = 3 then b.TimeEntryDate else null end) end,--MOD005
'FEES',
'Regular',
b.projectcode,
b.taskuid,
f.outlinenum,
f.taskname,
b.ActivityCode,
c.ActivityDesc,
b.resourceID,
RES.OrganizationID,
e.firstname,
e.lastname,
'','', -- expense
sum(isnull(b.StandardHours,0)), -- MOD003 - added in isnull's
0,--h.StandardAmt,--b.NegotiatedChargeRate, --MOD005 Change to NegotiatedChargeRate from StandardChargeRate
0,--sum(isnull(b.StandardHours,0)* IsNull(h.standardAmt,0)),--sum(bd.BilledAmt),--MOD005 Change from BillableAmt feild (was incorrect for adjustments)
case when @TimeDetail = 3 then b.invoicecomment else '' end,--MOD005
case when @TimeTypeGroup = 2 then b.TimeTypeCode else '' end--MOD008

from time b 
join activity c
on b.activitycode = c.activitycode
join resource e 
on b.resourceID = e.resourceID
join project p 
on b.ProjectCode=p.ProjectCode 
and p.RevisionStatusCode='A'
join task f 
on b.projectcode = f.projectcode 
and b.taskuid =f.taskuid 
and f.revisionnum = p.RevisionNum
join SCWeekEnding g   
on b.TimeEntryDate between g.StartDate and g.EndDate
join #Resources RES
on b.ResourceID = RES.ResourceID
--left join ratesetresource h on h.resourceid = b.resourceid
where       --b.projectcode = @PROJECTCODE and
b.statuscode in ('A','V','T')
and b.TimeEntryDate >= @FromDate
and b.TimeEntryDate <= @ToDate
and Isnull(b.StandardHours,0) <> 0
and b.resourceid in(Select ResourceId from #Resources)


group by  
b.projectcode,
b.taskuid,
f.outlinenum,
f.taskname,
b.ActivityCode,
c.ActivityDesc,
b.resourceID,
RES.OrganizationID,
e.firstname,
e.lastname,

case when @TimeDetail = 2 then g.enddate else (case when @TimeDetail = 3 then b.TimeEntryDate else null end) end,--MOD005
case when @TimeDetail = 3 then b.invoicecomment else '' end,
Case when @IncludeID = 1 then b.timeID else '' end, --mod 07
case when @TimeTypeGroup = 2 then b.TimeTypeCode else '' end--MOD008
having sum(isnull(b.StandardHours,0)) <> 0 


-- get actual time - OVERTIME
insert into #ItemisedTimeandMaterials
select      
Case when @IncludeID = 1 then b.timeID else '' end, --mod 07
e.lastname + e.firstname, 
case when @TimeDetail = 2 then g.enddate else (case when @TimeDetail = 3 then b.TimeEntryDate else null end) end,--MOD005
'FEES',
'Overtime',
--GW: need projectcode here--DONE
b.projectcode,
b.taskuid,
f.outlinenum,
f.taskname,
b.ActivityCode,
c.ActivityDesc,
b.resourceID,
RES.OrganizationID as OrgUnit,
e.firstname,
e.lastname,
'','', -- expense
sum(isnull(b.OvertimeHours,0)), -- MOD003 - added in isnull's
0,
0,
case when @TimeDetail = 3 then b.invoicecomment else '' end, --MOD005
case when @TimeTypeGroup = 2 then b.TimeTypeCode else '' end--MOD008
from time b 
join activity c 
on b.activitycode = c.activitycode
join resource e 
on b.resourceID = e.resourceID
join project p 
on b.ProjectCode=p.ProjectCode 
and p.RevisionStatusCode='A'
join task f
on b.projectcode = f.projectcode 
and b.taskuid =f.taskuid 
and f.revisionnum = p.RevisionNum
join SCWeekEnding g   
on b.TimeEntryDate between g.StartDate and g.EndDate
join #Resources RES
on b.ResourceID = RES.ResourceID

where       
b.statuscode in ('A','V','T')
--and f.revisionnum = @latestapprovedrevision
and b.TimeEntryDate >= @FromDate
and b.TimeEntryDate <= @ToDate
and Isnull(b.OvertimeHours,0) <> 0
and b.resourceid in (select resourceid from #Resources)

group by  
--GW: projectcode here--DONE
b.projectcode,
b.taskuid,
f.outlinenum,
f.taskname,
b.ActivityCode,
c.ActivityDesc,
b.resourceID,
RES.OrganizationID,
e.firstname,
e.lastname,

case when @TimeDetail = 2 then g.enddate else (case when @TimeDetail = 3 then b.TimeEntryDate else null end) end,--MOD005
case when @TimeDetail = 3 then b.invoicecomment else '' end,
case when @IncludeID = 1 then b.TimeID else '' end ,--mod 07
case when @TimeTypeGroup = 2 then b.TimeTypeCode else '' end--MOD008
having sum(isnull(b.Overtimehours,0)) <> 0

/**************** SECTION 7: OUTPUT DATA TO CRYSTAL REPORT ******************/

-- return data to the Crystal report only if taskuid are in table 1

select * 
from #ItemisedTimeandMaterials
order by taskUID

drop table #Resources
drop table #ItemisedTimeandMaterials

END

GO

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    #ItemisedTimeandMaterials 临时表中有 23 列,而您只从选择列表中插入 22 个值。

    当从 SELECT 列表中插入具有计算值的大量列的表时,您可以通过为每个计算列指定与目标临时表列相同的别名来跟踪列。

    通过这种方式,您可以轻松比较表格列和选择列表中的列并找出缺失的列。

    缺少的列是 EffectiveDate ,将其添加到正确位置的选择列表中

    Select
    ..
    ..
    b.resourceID,
    RES.OrganizationID,
    RES.EffectiveDate, --- Missing column
    e.firstname,
    e.lastname, 
    ...
    ..
    

    【讨论】:

    • 之前它工作得很好,我刚刚从 Create Table #Resources 添加了 EffectiveDate。所以我在 #ItemisedTimeandMaterials 临时表中添加了 EffectiveDate 资源。所以我认为列和值是平衡的。
    • #ItemisedTimeandMaterials 表中插入时,选择列表中缺少有效日期
    • 感谢 Mhasan 帮我解决这个问题!我是我们公司唯一的初级 BI 开发人员,老实说……我非常需要帮助。谢谢大家!
    【解决方案2】:

    检查表和查询(数据类型和值提供的长度)是否相同

    【讨论】:

    • 存储过程之前工作正常,但是当我添加 EffectiveDate datetime 和 Max(effectiveDate) 时,我得到了错误。在主表 ResourceOrganization 中,EffectiveDate 列的数据类型是日期时间。在进行插入时,我可以使用任何其他可以接受记录的数据类型吗?
    【解决方案3】:

    试试这个

    if @ResourceID <> ''
        begin
            insert into #Resources (ResourceID,OrganizationID,EffectiveDate)
    
                Select ro.ResourceID,
                       ro.OrganizationID,
                       ro.EffectiveDate
                from ResourceOrganization ro
                inner join (select ResourceID,
                                   MAX(EffectiveDate) as maxEffectivedate
                            from dbo.ResourceOrganization
                            where ResourceID = @ResourceID
                            group by ResourceID) as maxresults on ro.ResourceID = maxresults.ResourceID
                                                              and ro.EffectiveDate = maxresults.maxEffectivedate
        end
    

    编辑: 尝试像这一行一样执行它

    EXEC dbo.ExportResourceTime @ResourceID = N'ABC111', @OrgUnit = N'', @TimeDetail = 3, @ExpenseDetail = 1, @FromDate = '2013-10-01 00:00:00', @ToDate = '2013-10-30 00:00:00', @IncludeID = 1, @TimeTypeGroup = 21
    

    我没有你的数据库,所以很难从世界的另一端发现故障。

    我已经对你的脚本进行了一些格式化,但是看不出有什么问题,由于我上面提到的问题,我也无法运行脚本

    CREATE PROCEDURE [dbo].ExportResourceTime
        @ResourceID NVARCHAR(30),
        @OrgUnit NVARCHAR(15),
        @TimeDetail INT,
        @ExpenseDetail INT,
        @FromDate DATETIME,
        @ToDate DATETIME,
        @IncludeID INT,
        @TimeTypeGroup INT
                --1 = No Time Type Group
                --2 = Group by Time Type
    AS 
        BEGIN 
            CREATE TABLE #ItemisedTimeandMaterials
                (
                 IDNo INT,
                 OrderBy1 VARCHAR(60),
                 ItemDate DATETIME,--MOD005
                 RevenueTypeCode VARCHAR(24),
                 TimeType VARCHAR(24),
                 ProjectCode VARCHAR(20),
                 taskUID INT,
                 OutlineNum VARCHAR(60),
                 taskname VARCHAR(60),
                 activitycode VARCHAR(24),
                 ActivityDesc VARCHAR(60),
                 ResourceID NVARCHAR(24),
                 OrganizationID NVARCHAR(15),
                 EffectiveDate DATETIME,
                 firstname VARCHAR(60),
                 lastname VARCHAR(60),
                 ExpenseTypeCode VARCHAR(24),
                 ExpenseTypeDesc VARCHAR(60),
                 Hours DECIMAL(8, 2),
                 Rate DECIMAL(8, 2),
                 Total DECIMAL(20, 8),
                 Descr VARCHAR(256), --MOD005 DM Added col for relevant detail for Expenses
                 TimeTypeCode NVARCHAR(10)
                )
    --GW: move this bit to the top--DONE
    
            CREATE TABLE #Resources
                (
                 ResourceID NVARCHAR(30),
                 OrganizationID NVARCHAR(15),
                 EffectiveDate DATETIME
                )
    
    
            IF @ResourceID <> '' 
                BEGIN
                    INSERT  INTO #Resources
                            (ResourceID,
                             OrganizationID,
                             EffectiveDate)
                            SELECT  ro.ResourceID,
                                    ro.OrganizationID,
                                    ro.EffectiveDate
                            FROM    ResourceOrganization ro,
                                    (SELECT ResourceID,
                                            MAX(EffectiveDate) AS maxEffectivedate
                                     FROM   dbo.ResourceOrganization
                                     WHERE  ResourceID = @ResourceID
                                     GROUP BY ResourceID) AS maxresults
                            WHERE   ro.ResourceID = maxresults.ResourceID
                                    AND ro.EffectiveDate = maxresults.maxEffectivedate
                END
    
            IF @OrgUnit <> '' 
                BEGIN
                    INSERT  INTO #Resources
                            (ResourceID,
                             OrganizationID,
                             EffectiveDate)
                            SELECT  ResourceID,
                                    OrganizationID,
                                    EffectiveDate
                            FROM    ResourceOrganization
                            WHERE   OrganizationID LIKE '' + @OrgUnit + '%'
                END
    
    -- get actual time - REGULAR
            INSERT  INTO #ItemisedTimeandMaterials
                    SELECT  CASE WHEN @IncludeID = 1 THEN b.timeID
                                 ELSE ''
                            END, --mod 07
                            e.lastname + e.firstname,
                            CASE WHEN @TimeDetail = 2 THEN g.enddate
                                 ELSE (CASE WHEN @TimeDetail = 3 THEN b.TimeEntryDate
                                            ELSE NULL
                                       END)
                            END,--MOD005
                            'FEES',
                            'Regular',
                            b.projectcode,
                            b.taskuid,
                            f.outlinenum,
                            f.taskname,
                            b.ActivityCode,
                            c.ActivityDesc,
                            b.resourceID,
                            RES.OrganizationID,
                            e.firstname,
                            e.lastname,
                            '',
                            '', -- expense
                            SUM(ISNULL(b.StandardHours, 0)), -- MOD003 - added in isnull's
                            0,--h.StandardAmt,--b.NegotiatedChargeRate, --MOD005 Change to NegotiatedChargeRate from StandardChargeRate
                            0,--sum(isnull(b.StandardHours,0)* IsNull(h.standardAmt,0)),--sum(bd.BilledAmt),--MOD005 Change from BillableAmt feild (was incorrect for adjustments)
                            CASE WHEN @TimeDetail = 3 THEN b.invoicecomment
                                 ELSE ''
                            END,--MOD005
                            CASE WHEN @TimeTypeGroup = 2 THEN b.TimeTypeCode
                                 ELSE ''
                            END--MOD008
                    FROM    time b
                    JOIN    activity c ON b.activitycode = c.activitycode
                    JOIN    resource e ON b.resourceID = e.resourceID
                    JOIN    project p ON b.ProjectCode = p.ProjectCode
                                         AND p.RevisionStatusCode = 'A'
                    JOIN    task f ON b.projectcode = f.projectcode
                                      AND b.taskuid = f.taskuid
                                      AND f.revisionnum = p.RevisionNum
                    JOIN    SCWeekEnding g ON b.TimeEntryDate BETWEEN g.StartDate AND g.EndDate
                    JOIN    #Resources RES ON b.ResourceID = RES.ResourceID
    --left join ratesetresource h on h.resourceid = b.resourceid
                    WHERE   --b.projectcode = @PROJECTCODE and
                            b.statuscode IN ('A', 'V', 'T')
                            AND b.TimeEntryDate >= @FromDate
                            AND b.TimeEntryDate <= @ToDate
                            AND ISNULL(b.StandardHours, 0) <> 0
                            AND b.resourceid IN (SELECT ResourceId
                                                 FROM   #Resources)
                    GROUP BY b.projectcode,
                            b.taskuid,
                            f.outlinenum,
                            f.taskname,
                            b.ActivityCode,
                            c.ActivityDesc,
                            b.resourceID,
                            RES.OrganizationID,
                            e.firstname,
                            e.lastname,
                            CASE WHEN @TimeDetail = 2 THEN g.enddate
                                 ELSE (CASE WHEN @TimeDetail = 3 THEN b.TimeEntryDate
                                            ELSE NULL
                                       END)
                            END,--MOD005
                            CASE WHEN @TimeDetail = 3 THEN b.invoicecomment
                                 ELSE ''
                            END,
                            CASE WHEN @IncludeID = 1 THEN b.timeID
                                 ELSE ''
                            END, --mod 07
                            CASE WHEN @TimeTypeGroup = 2 THEN b.TimeTypeCode
                                 ELSE ''
                            END--MOD008
                    HAVING  SUM(ISNULL(b.StandardHours, 0)) <> 0 
    
    
    -- get actual time - OVERTIME
            INSERT  INTO #ItemisedTimeandMaterials
                    SELECT  CASE WHEN @IncludeID = 1 THEN b.timeID
                                 ELSE ''
                            END, --mod 07
                            e.lastname + e.firstname,
                            CASE WHEN @TimeDetail = 2 THEN g.enddate
                                 ELSE (CASE WHEN @TimeDetail = 3 THEN b.TimeEntryDate
                                            ELSE NULL
                                       END)
                            END,--MOD005
                            'FEES',
                            'Overtime',
    --GW: need projectcode here--DONE
                            b.projectcode,
                            b.taskuid,
                            f.outlinenum,
                            f.taskname,
                            b.ActivityCode,
                            c.ActivityDesc,
                            b.resourceID,
                            RES.OrganizationID AS OrgUnit,
                            e.firstname,
                            e.lastname,
                            '',
                            '', -- expense
                            SUM(ISNULL(b.OvertimeHours, 0)), -- MOD003 - added in isnull's
                            0,
                            0,
                            CASE WHEN @TimeDetail = 3 THEN b.invoicecomment
                                 ELSE ''
                            END, --MOD005
                            CASE WHEN @TimeTypeGroup = 2 THEN b.TimeTypeCode
                                 ELSE ''
                            END--MOD008
                    FROM    time b
                    JOIN    activity c ON b.activitycode = c.activitycode
                    JOIN    resource e ON b.resourceID = e.resourceID
                    JOIN    project p ON b.ProjectCode = p.ProjectCode
                                         AND p.RevisionStatusCode = 'A'
                    JOIN    task f ON b.projectcode = f.projectcode
                                      AND b.taskuid = f.taskuid
                                      AND f.revisionnum = p.RevisionNum
                    JOIN    SCWeekEnding g ON b.TimeEntryDate BETWEEN g.StartDate AND g.EndDate
                    JOIN    #Resources RES ON b.ResourceID = RES.ResourceID
                    WHERE   b.statuscode IN ('A', 'V', 'T')
    --and f.revisionnum = @latestapprovedrevision
                            AND b.TimeEntryDate >= @FromDate
                            AND b.TimeEntryDate <= @ToDate
                            AND ISNULL(b.OvertimeHours, 0) <> 0
                            AND b.resourceid IN (SELECT resourceid
                                                 FROM   #Resources)
                    GROUP BY  
    --GW: projectcode here--DONE
                            b.projectcode,
                            b.taskuid,
                            f.outlinenum,
                            f.taskname,
                            b.ActivityCode,
                            c.ActivityDesc,
                            b.resourceID,
                            RES.OrganizationID,
                            e.firstname,
                            e.lastname,
                            CASE WHEN @TimeDetail = 2 THEN g.enddate
                                 ELSE (CASE WHEN @TimeDetail = 3 THEN b.TimeEntryDate
                                            ELSE NULL
                                       END)
                            END,--MOD005
                            CASE WHEN @TimeDetail = 3 THEN b.invoicecomment
                                 ELSE ''
                            END,
                            CASE WHEN @IncludeID = 1 THEN b.TimeID
                                 ELSE ''
                            END,--mod 07
                            CASE WHEN @TimeTypeGroup = 2 THEN b.TimeTypeCode
                                 ELSE ''
                            END--MOD008
                    HAVING  SUM(ISNULL(b.Overtimehours, 0)) <> 0
    
    /**************** SECTION 7: OUTPUT DATA TO CRYSTAL REPORT ******************/
    
    -- return data to the Crystal report only if taskuid are in table 1
    
            SELECT  *
            FROM    #ItemisedTimeandMaterials
            ORDER BY taskUID
    
            DROP TABLE #Resources
            DROP TABLE #ItemisedTimeandMaterials
    
        END
    
    GO
    

    【讨论】:

    • @BA82283 看看编辑,但它说什么..我第一次尝试时遇到的错误是什么?
    • 表示高亮显示有错误 -- 获取实际时间 -- REGULAR INSERT INTO #ItemisedTimeandMaterials Msg 213, Level 16, State 1, Procedure ExportResourceTime, Line 86 Insert Error: Column name or number of提供的值与表定义不匹配。
    • 皮埃尔,我在 -- 获取实际时间 - OVERTIME 临时表下缺少 RES.EffectiveDate。谢谢你帮我解决这个问题。我很感激时间和精力。让我知道我可以如何投票给你,或者我可以为你做些什么作为回报。
    • 我很高兴你被排序了。由于我没有帮助您解决问题,因此无需投票
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2016-04-17
    • 2013-11-09
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    相关资源
    最近更新 更多