从存储过程插入临时表会导致错误

时间:2017-11-20 09:54:02

标签: sql sql-server sql-server-2012

我有这个存储过程:

ALTER Procedure [dbo].[sp_Prd_Dashboard_Summary]
    (@Period AS INT)
AS
    SELECT 
        SiteName AS SiteName, 
        MAX(Country) AS Country, 
        BudgetPrj, 
        MAX(PeriodEnd) AS PeriodEnd, 
        MAX(DaysMtd) AS DaysMtd,
        MAX(ToGoMtd) AS ToGoMtd, 
        MAX(PeriodToTDays) AS PeriodToTDays,
        SUM(MTDRevenue) AS MtdRev, 
        SUM(MTDRevenue) / NULLIF(MAX(DaysMTD), 0) * MAX(PeriodToTDays) AS PrjRevenue,
        SUM(BdgRevenue) AS BdgRev, SUM(TrgRevenue) AS TrgRev,
        SUM(BCMMtd) AS BCMMtd, SUM(HrsMtd) AS HrsMTD, 
        SUM(FuelVal) AS FuelVal, SUM(FuelLtrs) AS FuelLtrs,
        SUM(FuelVal) / NULLIF(SUM(MTDRevenue), 0) AS FuelPerc
    FROM
        (SELECT
             St.SiteName as SiteName,
             St.Country as Country, 
             Null as BudgetPrj, Prd.PeriodEnd as PeriodEnd,
             Day(GetDate()) as DaysMtd, 
             Prd.PeriodNoDays - Day(GetDate()) as ToGoMtd,
             Prd.PeriodNoDays as PeriodToTDays,
             0 as MTDRevenue, 0 as BdgRevenue, 
             0 as TrgRevenue, 0 as BCMMtd,
             0 as HrsMtd, 0 as FuelVal,
             0 as FuelLtrs
         FROM 
             Periods Prd 
         JOIN
             Sites St ON Prd.PeriodSiteID = St.SiteId
         WHERE
             Prd.Period = @Period AND St.SiteActive = 1

         UNION All

    Select SiteName as SiteName
    , Dit.Country as Country
    , Null as BudgetPrj
    , Null as PeriodEnd
    , 0 DaysMtd
    , 0 as ToGoMtd
    , 0 as PeriodToTDays
    , IIF(Dit.Wcode = 101, 
            IIF(DiT.WBillMeth = 'Hours', DiT.Hrs * DiT.OpBill,
                IIF(DiT.WBillMeth = 'BCM', Loads * DiT.ModelSize * DiT.WBillRate,
                    IIF(DiT.WBillMeth = 'Cost Plus', (DiT.Hrs * (DiT.OwnBill + DiT.OpBill)) +
                        (DiT.ShiftHrs * DiT.EmpBill),0))),0) as MTDRevenue
    , 0 as BdgRevenue
    , 0 as TrgRevenue
    , IIF(DiT.WBillMeth = 'BCM', Loads * DiT.ModelSize, 0) as BCMMtd
    , IIF(Dit.Wcode = 101, 
            IIF(DiT.WBillMeth <> 'BCM', DiT.Hrs, 0),0) as HrsMtd
    , DiT.Fuel * DiT.FuelRate as FuelVal
    , DiT.Fuel as FuelLtrs

    From DataInputTotal DiT

    Where DiT.Period = @Period and DiT.SiteActive = 1

    Union All

    Select SiteName as SiteName
    , St.Country as Country
    , Bdgt.BudgetProject as BudgetPrj
    , Prd.PeriodEnd as PeriodEnd
    , 0 as DaysMtd
    , 0 as ToGoMtd
    , 0 as PeriodToTDays
    , 0 as MTDRevenue
    , PrjRev as BdgRevenue
    , BudgTarget as TrgRevenue
    , 0 as BCMMtd
    , 0 as HrsMtd
    , 0 as FuelVal
    , 0 as FuelLtrs

    From Budget Bdgt Join 
        Sites St on Bdgt.SiteId = St.SiteId Join
        Periods Prd on Bdgt.Period = prd.Period and Bdgt.SiteId = Prd.PeriodSiteID

    Where Bdgt.Period = @Period and St.SiteActive = 1

) a


Group By SiteName, BudgetPrj

我正在尝试调用该过程并使用以下脚本将结果插入临时表:

Declare @Period int = 22
Declare @DaysinMonth Int = 29
Declare @DayHrs Int = 24

IF OBJECT_ID('tempdb..#Temp') IS NOT NULL DROP TABLE #Temp

Create Table #Temp (SiteName nvarchar(50)
                    , Country nvarchar(50)
                    , BudgetPrj nvarchar(50)
                    , PeriodEnd DateTime
                    , DaysMtd Int
                    , ToGoMtd Int
                    , PeriodToTDays Int
                    , MtdRev Numeric(13,2)
                    , PrjRevenue Numeric(13,2)
                    , BdgRev Numeric(13,2)
                    , TrgRev Numeric(13,2)
                    , BCMMtd Numeric(13,2)
                    , HrsMtd Numeric(13,2)
                    , FuelVal Numeric(13,2)
                    , FuelLtrs Numeric(13,2)
                    , FuelPerc Numeric(13,2)
                    , FltCnt Int
                    , Availibility Numeric(13,2)
                    , Utilization Numeric(13,2)
                    , Idle Numeric(13,2)
                    )

Insert #temp
Exec sp_Prd_Dashboard_Summary @Period
Insert into #temp
Exec summary_fleet_performance @DayHrs, @Period, @DaysinMonth

运行脚本时出现以下错误:

  

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

我检查了SELECT的别名,所有列都有名称。

3 个答案:

答案 0 :(得分:1)

您尝试插入行的临时表包含的列数多于存储过程的结果。在插入查询中提供列名称,如下所示:

Insert into table(Column1, Column2....)

答案 1 :(得分:1)

尝试:您的存储过程返回14列,临时表包含更多列,因此您必须在INSERT INTO #TEMP中提及如下所示的columnS,如果您没有指定列表格的名称然后从STORED PROCEDURE返回列也必须相同。

INSERT INTO #temp(SiteName
    , Country
    , BudgetPrj
    , PeriodEnd
    , DaysMtd
    , ToGoMtd
    , PeriodToTDays
    , MtdRev
    , PrjRevenue
    , BdgRev
    , TrgRev
    , BCMMtd
    , HrsMtd
    , FuelVal
    , FuelLtrs)
Exec sp_Prd_Dashboard_Summary @Period

答案 2 :(得分:1)

我结束了创建两个临时表并将每个存储过程插入每个表然后加入它们。

它对我很有用:

...

'<div id="C" class="confirmBoxBtn" onclick="AlertC()"> AlertC </div>' +
'<script>function AlertC() { alert("Hello from C"); }</script>' +

...