【问题标题】:Toad For Oracle: Bind Variable "End_Year" not DeclaredToad For Oracle:未声明绑定变量“End_Year”
【发布时间】:2017-05-01 14:56:39
【问题描述】:

我使用来自几个不同帖子的建议拼凑了以下代码,但遇到了困难。我对这段代码的最终目标是查找从去年 10 月 1 日到当年 9 月 30 日的记录,而无需提示用户输入或必须在 between 语句中硬编码日期范围。我目前在运行代码时收到以下错误“绑定变量“End_Year”未声明”。

    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

【问题讨论】:

  • :var_name 是一个绑定变量 : 告诉编译器期待用户输入。更改您的 var_names 以删除 : 假设定义的 end_year 和 Begin_year 变量不是 hb_invoicing_All 中的列名...否则还要重命名变量。喜欢var_Begin_yearstackoverflow.com/questions/1597806/…

标签: sql oracle toad


【解决方案1】:

您的查询有几个问题,首先是将alias 赋予into 子句。此外,您必须将所有语句封装在 1 PLSQL 块内。

您还打算如何处理选择查询。您是否在其他地方显示输出?

最简单的方法是直接使用下面的查询,而不是使用 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 块中。

【讨论】:

    【解决方案2】:

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

    现在关于你的主要目标

    我对这段代码的最终目标是查找从去年 10 月 1 日开始的记录 年到当年的 9 月 30 日,而不提示用户 在 between 语句中输入或必须对日期范围进行硬编码

    鉴于 去年 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
    

    这只是另一种可能更'程序员友好'的方法。

    如果您决定在某一天查看您的日期 - 您不必计算 trunc(sysdate, 'YEAR')-92 中的 9392 代表什么(您的方法)。

    【讨论】:

      猜你喜欢
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多