【问题标题】:Use for loop in Oracle SQL developer在 Oracle SQL 开发人员中使用 for 循环
【发布时间】:2019-08-23 10:19:06
【问题描述】:

下面的代码给了我这个错误

ORA-00933: SQL 命令未正确结束 00933. 00000 - “SQL 命令未正确结束” *原因:
*行动: 行错误:26 列:1

for i in (7,14,21,28,35) 
loop
  Select CODE_ACCOUNTING_METHOD,
         trunc(sysdate-i, 'iw')           as week_report,
         sum(ELI_12MOB)                   as eligible_client,
         sum(client)                      as total_client,
         (sum(ELI_12MOB)/sum(client))*100 as eligible_rate
   from  pm_eli_base
   where week_start = trunc(sysdate-(360+i), 'iw')
     and code_accounting_method != 'CL'
   group by CODE_ACCOUNTING_METHOD, trunc(sysdate-i, 'iw')
   union all
   select 'Total', 
           trunc(sysdate-i, 'iw') as week_report,
           sum(ELI_12MOB)         as eligible_client,
           sum(client)            as total_client,
           (sum(ELI_12MOB)/sum(client))*100
     from pm_eli_base
    where week_start = trunc(sysdate-(360+i), 'iw')
      and code_accounting_method != 'CL'
    group by 'Total', trunc(sysdate-i, 'iw')
end loop;

请帮我解决这个问题

This is the result I want to achieve

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    由于您使用的是 SQLDeveloper,因此您可以在游标中声明您的查询,并在 i 的选择值上调用您的游标

    DECLARE 
    
    CURSOR code_accounting_method(i number)
    is
    Select CODE_ACCOUNTING_METHOD,
             trunc(sysdate-i, 'iw')           as week_report,
             sum(ELI_12MOB)                   as eligible_client,
             sum(client)                      as total_client,
             (sum(ELI_12MOB)/sum(client))*100 as eligible_rate
       from  pm_eli_base
       where week_start = trunc(sysdate-(360+i), 'iw')
         and code_accounting_method != 'CL'
       group by CODE_ACCOUNTING_METHOD, trunc(sysdate-i, 'iw')
       union all
       select 'Total', 
               trunc(sysdate-i, 'iw') as week_report,
               sum(ELI_12MOB)         as eligible_client,
               sum(client)            as total_client,
               (sum(ELI_12MOB)/sum(client))*100
         from pm_eli_base
        where week_start = trunc(sysdate-(360+i), 'iw')
          and code_accounting_method != 'CL'
        group by 'Total', trunc(sysdate-i, 'iw');
    
     cam_record  CODE_ACCOUNTING_METHOD%rowtype;
    
    BEGIN
    
            for i in 1 .. 35 loop
              if i in (7,14,21,28,35) then
                    open code_accounting_method(i);
                    fetch code_accounting_method into  cam_record;
                    close code_accounting_method;
                   --Now report the fields that are in cam_record;
                   -- such as 
                  dbms_output.put_line(cam_record.week_report);
    
              end if;
            end loop;
    END;      
    

    【讨论】:

      【解决方案2】:

      你不能这样做:

      for i in (7,14,21,28,35)
      

      做:

      SQL> 
      declare
         type nt_type is table of number;
         nt nt_type := nt_type (7,14,21,28,35);
      begin
      for i in 1..nt.count loop
         dbms_output.put_line(nt(i));
      end loop;
      end;
      /
      
      7
      14
      21
      28
      35
      
      PL/SQL procedure successfully completed.
      

      【讨论】:

      • 谢谢兄弟,当我将 select 语句放入循环时,它给了我这个错误错误报告 - ORA-06550:第 6 行,第 1 列:PLS-00428:此 SELECT 中应有一个 INTO 子句语句 06550. 00000 - “行 %s,列 %s:\n%s” *原因:通常是 PL/SQL 编译错误。 *行动:
      • 正如 Tejash 所说,您需要使用 INTO 子句将查询结果分配给变量。
      【解决方案3】:

      首先你在查询结束时错过了;

      其次,您想通过选择查询来实现什么?使用INTO 为某个变量赋值。

      更新: 除了@btpys 建议的内容之外,您还可以使用以下技术。

      FOR J IN 1..5 LOOP
      I := J*7;
      ...
      ...
      END LOOP;
      

      干杯!!

      【讨论】:

      • 我已经添加了我想要达到的结果
      【解决方案4】:

      我认为for 循环是错误的工具。 您应该创建一个视图,并使用 union all 并手动复制和粘贴并将 i 替换为值 7,14,21,28,35。这样您就可以看到带有 union all 5x2 次 = 10 条选择语句的视图。

      【讨论】:

      • 我就是这么做的,但我觉得它看起来不那么干净,哈哈
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 2022-07-19
      • 2012-03-21
      • 2016-12-07
      • 2018-07-31
      • 1970-01-01
      相关资源
      最近更新 更多