2015-06-05 74 views
0

我有一個表函數,它包含一些CTE表,最終滾動到返回的select語句。在其中一個CTE表中,我使用@variable。它與我聲明的函數是相同的@變量。當我嘗試保存該函數時,出現此錯誤:cte tsql使用變量

Lookup Error - SQL Server Database Error: Parameters were not supplied for the function 'forecast_baseline'. 

在CTE表中使用@variable有問題嗎?我可以提供代碼,但它似乎沒有將函數@variable傳遞給CTE表,因此編譯器不喜歡它。

ALTER function [eo].[forecast_baseline] (@monthkey as char(6)) 
    returns @results table(
    [billing_date] date, 
    [year] int, 
    [type] varchar(max), 
    [dollars] float, 
    [units] float, 
    [CC] int, 
    [offering] varchar(max), 
    [IntegratedReleasePlanNm] varchar(max), 
    [ProjectId] varchar(max), 
    [ProjectNm] varchar(max), 
    [ModelEstimateId] varchar(max), 
    [query] varchar(max), 
    [ItemGroupId] varchar(max), 
    CashflowType varchar(12), 
    plotdate datetime 
) 
    as 
    begin 
    declare @StartTime datetime = (SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(getdate())-1),getdate()),101)) 
    declare @EndTime datetime = DATEADD(year, +5, @StartTime) 
    declare @Interval int = 1; 

    WITH yearsinmonths (
     [datetimemonth], 
     [enddatetimemonth]) AS ( 
      SELECT 
      @StartTime datetimemonth, 
      DATEADD(month, @Interval, @StartTime) AS enddatetimemonth 
      UNION ALL 
      SELECT 
      EndRange, 
      DATEADD(month, @Interval, enddatetimemonth) 
      FROM 
      cSequence 
      WHERE 
      DATEADD(month, @Interval, enddatetimemonth) < @EndTime 
    ), 

    forecast_baseline (
     [billing_date], 
     [year], 
     [type], 
     [dollars], 
     [units], 
     [CC], 
     [offering], 
     [IntegratedReleasePlanNm], 
     [ProjectId], 
     [ProjectNm], 
     [ModelEstimateId], 
     [query], 
     [ItemGroupId], 
     [CashflowType]) as (
      select 
      dateadd(month,-1,convert(date, a.CHARGE_MONTH+'01', 112)) 
      ,year(dateadd(month,-1,convert(date, a.CHARGE_MONTH+'01', 112))) 
      ,'Baseline' 
      ,sum(cast(a.CHARGE_AMOUNT as money)) 
      ,sum(cast (PPGUNITS as float)) 
      ,ORG_CC 
      ,[offering] 
      ,'Baseline' 
      ,'baseline' 
      ,'baseline' 
      ,'baseline' 
      ,'baseline' 
      ,'N/A' 
      ,'N/A' 
      from 
      [sources].[feeds].[MARS2IEO_MARS_BD12_INV_LOB_EXTRACTS] a join sources.[md].[MARS_ITEMID_MAPPING] b 
       on a.offering=b.itemid 
      where 
      host_name is not null 
      and (a.org_sort_code like ('KBBFA%') or 
       a.org_sort_code like ('KBBFB%') or 
       a.org_sort_code like ('KBBDD%')) 
      and b.category in ('server','disk','tape') 
      and cast(year(dateadd(month,-1,convert(date,  a.CHARGE_MONTH+'01', 112))) as char(4)) + RIGHT('00' + CONVERT(VARCHAR,month(dateadd(month,-1,convert(date, a.CHARGE_MONTH+'01', 112)))), 2) <= @monthkey -- this is the line that throws the error 
      group by 
      offering 
      ,ORG_CC 
      ,dateadd(month,-1,convert(date, a.CHARGE_MONTH+'01', 112)) 
      ,year(dateadd(month,-1,convert(date, a.CHARGE_MONTH+'01', 112))) 
) 

    INSERT INTO @results (
     [billing_date], 
     [year], 
     [type], 
     [dollars], 
     [units], 
     [CC], 
     [offering], 
     [IntegratedReleasePlanNm], 
     [ProjectId], 
     [ProjectNm], 
     [ModelEstimateId], 
     [query], 
     [ItemGroupId], 
     [CashflowType] 
    ) 
     select 
     * 
     from 
     forecast_baseline 

    INSERT INTO @results (
     [billing_date], 
     [year], 
     [type], 
     [dollars], 
     [units], 
     [CC], 
     [offering], 
     [IntegratedReleasePlanNm], 
     [ProjectId], 
     [ProjectNm], 
     [ModelEstimateId], 
     [query], 
     [ItemGroupId], 
     CashflowType 
    ) 
     SELECT 
     b.datetimemonth 
     ,year([billing_date]) 
     ,'Baseline' 
     ,[dollars] 
     ,[units] 
     ,CC 
     ,[offering] 
     ,'Baseline' 
     ,'projected' 
     ,'projected' 
     ,'projected' 
     ,'projected' 
     ,'N/A' 
     ,'N/A' 
     FROM 
     forecast_baseline a inner join yearsinmonths b 
      on cast(year([billing_date]) as char(4))+ RIGHT('00' + CONVERT(VARCHAR,month([billing_date])), 2) = @monthkey 
     and b.datetimemonth > [billing_date] 
     return 
    end 
+1

是的,請提供代碼請 –

+0

添加代碼。問題出在** forecast_baseline ** cte。它正在尋找一個參數,例如它不認可@monthkey作爲參數。我必須在那裏有變量,因爲這個cte從中拉出的表太大而無法加載整個事物。性能會坦克。 –

回答

2

錯誤是因爲您已命名爲CTE作爲您的函數名稱。更改第二個CTE名稱:

... 
WHERE 
DATEADD(month, @Interval, enddatetimemonth) < @EndTime 
), 

forecast_baseline (
... 

以任何其他名稱。

此外,您將需要使用UNION ALL而不是在結果表中單獨插入2個插入,因爲您只有1個可能性來查詢CTE。您無法多次查詢CTE

+0

哇。接得好。這甚至沒有發生在我身上。它應該有....我沒有意識到它正在遞歸。非常感謝! –

+0

@ M.Ford,不客氣 –