Toad For Oracle:未声明绑定变量 "End_Year"

Toad For Oracle: Bind Variable "End_Year" not Declared

我根据几个不同 post 的建议拼凑了以下代码,但遇到了困难。此代码的最终目标是查找从去年 10 月 1 日到当年 9 月 30 日的记录,而无需提示用户输入或必须在 between 语句中对日期范围进行硬编码。我目前收到以下错误 "Bind variable "End_Year"not declared" when 运行 the code.

    declare
begin_Year date;
begin
 select trunc(sysdate, 'YEAR')-92 FY_begin_year
  Into begin_Year
  from Dual;
 end;

 declare
 End_Year date;
 begin
select  add_months(trunc(sysdate, 'YEAR'), 12)-93 FY_end_year
into End_Year
from dual;
end;

SELECT inv.company as company
               , inv.customer_id as cust
               , inv.address_id
               ,inv.invdate
               , SUM(inv.sales) as sales
               , SUM(inv.cost) as costs 
               FROM ifsinfo.hb_invoicing_all inv 
            WHERE inv.site IN ('06','01')
                AND TO_DATE(inv.invdate) between :begin_Year and :End_Year
            GROUP BY inv.company
               , inv.customer_id
               , inv.address_id 
               , inv.invdate

您的查询有几处错误,首先是 aliasinto 子句。您还必须将所有语句封装在 1 PLSQL 块中。

另外,您打算如何处理 select 查询。您是否在其他地方显示输出?

最简单的方法是直接使用下面的查询,而不是使用 2 个不同的变量。

SELECT inv.company as company
   , inv.customer_id as cust
   , inv.address_id
   ,inv.invdate
   , SUM(inv.sales) as sales
   , SUM(inv.cost) as costs 
   FROM ifsinfo.hb_invoicing_all inv 
WHERE inv.site IN ('06','01')
    AND TO_DATE(inv.invdate) 
    between  
        trunc(sysdate, 'YEAR')-92  
        and 
        add_months(trunc(sysdate, 'YEAR'), 12)-93
GROUP BY inv.company
   , inv.customer_id
   , inv.address_id 
   , inv.invdate

如果您绝对必须使用变量,请将它们放在同一个 plsql 块中。

如其他人所述 - 您不需要任何 plsql 块 - 只需一个 sql 查询。

现在关于你的主要目标

My ultimate goal for this code is to find records from Oct 1st of last year to Sep 30th of the current year without prompting the user for input or having to hard code the date range in the between statement

鉴于 去年 10 月 1 日 可以找到

to_date('01/10/'|| (to_number(to_char(sysdate,'YYYY'))-1),'dd/mm/rrrr')

当年9月30日

to_date('30/09/'|| to_char(sysdate,'YYYY'),'dd/mm/rrrr')

您的查询变为

SELECT inv.company as company
   , inv.customer_id as cust
   , inv.address_id
   ,inv.invdate
   , SUM(inv.sales) as sales
   , SUM(inv.cost) as costs 
   FROM ifsinfo.hb_invoicing_all inv 
WHERE inv.site IN ('06','01')
    AND TO_DATE(inv.invdate) 
    between  
        to_date('01/10/'|| (to_number(to_char(sysdate,'YYYY'))-1),'dd/mm/rrrr')  
        and 
        to_date('30/09/'|| to_char(sysdate,'YYYY'),'dd/mm/rrrr')
GROUP BY inv.company
   , inv.customer_id
   , inv.address_id 
   , inv.invdate

这只是另一种方法,可能会多一点'programmer friendly'

如果您决定有一天回顾您的约会 - 您将不必计算 9392trunc(sysdate, 'YEAR')-92 中代表什么(您的方法)。