【问题标题】:Stored Procedure Error: Column name or number of supplied values does not match table definition存储过程错误:列名或提供的值的数量与表定义不匹配
【发布时间】:2013-12-25 08:45:45
【问题描述】:

它说我有错误

 where ResourceID = @ResourceID

但我没有看到任何东西。

请参阅下面的整个查询。我独立运行了它,它可以工作。

这是我刚刚运行的独立查询。

独立查询

  Select  
      ResourceID, OrganizationID 
  from 
      ResourceOrganization
  where 
      ResourceID = 'FA11343' 
      and EffectiveDate = (select MAX(EffectiveDate) 
                           from ResourceOrganization 
                           where ResourceID = 'FA11343' 
                           group by ResourceID)

涉及错误的实际查询

 if @ResourceID <> ''
 begin
     insert into #Resources (resourceID, OrganizationID)
        Select  
            ResourceID, OrganizationID 
        from 
            ResourceOrganization
        where 
            ResourceID = @ResourceID
            and EffectiveDate = (select MAX(EffectiveDate) 
                                 from ResourceOrganization 
                                 where ResourceID = @ResourceID
                                 group by ResourceID)
 end

create procedure [dbo].[ExportResourceTime]
    @ResourceID nvarchar(30),
    @OrgUnit nvarchar(15),
    @TimeDetail int,
    @ExpenseDetail int,
    @FromDate Datetime,
    @ToDate Datetime,
    @IncludeID int,
    @TimeTypeGroup int
as
BEGIN 
 --SET @ResourceID = 'DM2699'
 --SET @OrgUnit = ''
 --SET @TimeDetail = 3
 --SET @ExpenseDetail = 1
 --SET @FromDate = '2013-11-01'
 --SET @ToDate = '2013-11-30'
 --SET @IncludeID = 1
 --SET @TimeTypeGroup = 21

/************************** SECTION 1 - CREATING A TEMP TABLE TO GET TIME AND EXPENSE DETAILS  ********************/
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 varchar(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
)

--select * from #Resources

--insert into #Resources
-- 1. @resourceid is present then only 1 single record in the table
-- 2. if @orgunit  is present, find all resourceID belongs to this orgunit and insert into #resources

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

  Select  ResourceID,OrganizationID from ResourceOrganization
  where ResourceID = @ResourceID
  and EffectiveDate = (select MAX(EffectiveDate) 
  from ResourceOrganization 
  where ResourceID = @ResourceID
  group by ResourceID)
  end

 if @OrgUnit <> ''
 begin
 insert into #Resources (resourceID,OrganizationID)
  Select  A.ResourceID,A.OrganizationID from ResourceOrganization a
  where A.OrganizationID like '' + @OrgUnit + '%'
  and A.EffectiveDate = (select MAX(EffectiveDate) 
  from ResourceOrganization 
  where ResourceID = A.ResourceID 
  group by ResourceID)
  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 SMECWeekEnding 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)

--GW: need to filter by resources here      

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 SMECWeekEnding 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-2008


    【解决方案1】:

    您的临时表#Resources 要求所有字段都有值。在您的插入语句中,您没有提供 EffectiveDate。因此,要么提供该文件,要么将 Resources 表的定义更改为:

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

    【讨论】:

    • 嗨,Brendan,它没有用。它提示相同的错误,错误在第 103 行,即 if @ResourceID '' begin.. insert into #Resource 的“End”
    猜你喜欢
    • 2014-01-22
    • 2014-11-02
    • 2018-11-24
    • 1970-01-01
    • 2015-10-20
    • 2022-09-28
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多