在Pl / sql中打印数字的偶数和

时间:2020-11-08 13:22:33

标签: oracle plsql

enter image description here

我似乎无法解决此问题

2 个答案:

答案 0 :(得分:0)

从错误本身开始,这是因为您试图将整个temp,它被声明为游标变量-c_nm%rowtype)放入整数变量(i)。尽管temp实际上只包含一列-num,但它不起作用。

因此,屏幕截图的第15行:

No : i := temp;
Yes: i := temp.num;

修复后,您的代码即可,但是-不幸的是,产生了错误的结果。在输入数字121974553中,偶数分别是2、9、4和5,它们的总和等于20,而不是6(如您的结果所示):

SQL> set serveroutput on;
SQL> declare
  2    ad int := 0;
  3    i int;
  4    b int;
  5    cursor c_nm is select * From numb;
  6    temp c_nm%rowtype;
  7  begin
  8    open c_nm;
  9    loop
 10      fetch c_nm into temp;
 11      exit when c_nm%notfound;
 12      i := temp.num;                  --> this
 13
 14      while i > 0 loop
 15        b := mod(i, 10);
 16        if mod(b, 2) = 0 then
 17           ad := b + ad;
 18        end if;
 19        i := trunc(i/10);
 20      end loop;
 21    end loop;
 22    dbms_output.put_line('ad = ' ||ad);
 23    close c_nm;
 24  end;
 25  /
ad = 6

PL/SQL procedure successfully completed.

SQL>

一个更简单的选择可能是:将数字分成数字(每个数字都在其单独的行中),以便您可以将SUM函数应用于其偶数:

SQL> select * from numb;

       NUM
----------
 121974553     --> sum of even digits = 2 + 9 + 4 + 5 = 20
 253412648     -->                    = 5 + 4 + 2 + 4 = 15

SQL> with
  2  split as
  3    -- split number into rows
  4    (select num,
  5            substr(num, column_value, 1) digit,
  6            case when mod(column_value, 2) = 0 then 'Y'
  7                 else 'N'
  8            end cb_odd_even
  9     from numb cross join table(cast(multiset(select level from dual
 10                                              connect by level <= length(num)
 11                                             ) as sys.odcinumberlist))
 12    )
 13  -- final result: summary of digits in even rows
 14  select num,
 15         sum(digit)
 16  from split
 17  where cb_odd_even = 'Y'
 18  group by num;

       NUM SUM(DIGIT)
---------- ----------
 253412648         15
 121974553         20

SQL>

如果必须是PL / SQL,则将其稍微重写为

SQL> declare
  2    result number;
  3  begin
  4    for cur_r in (select num from numb) loop
  5      with
  6      split as
  7        -- split number into rows
  8        (select substr(cur_r.num, level, 1) digit,
  9                case when mod(level, 2) = 0 then 'Y'
 10                     else 'N'
 11                end cb_odd_even
 12         from dual
 13         connect by level <= length(cur_r.num)
 14        )
 15      -- final result: summary of digits in even rows
 16      select sum(digit)
 17      into result
 18      from split
 19      where cb_odd_even = 'Y';
 20
 21      dbms_output.put_line(cur_r.num || ' --> ' || result);
 22    end loop;
 23  end;
 24  /
121974553 --> 20
253412648 --> 15

PL/SQL procedure successfully completed.

SQL>

答案 1 :(得分:0)

您可以执行以下操作。注意事项:首先,可以使用隐式游标(join orders o on customers.customerNumber = orders.customerNumber join orderdetails o2 on orders.orderNumber = orderdetails.orderNumber join products p on orderdetails.productCode = products.productCode union select status from orders o where status = 'shipped' order by rand() limit 5; 构造)。您不需要声明SQL Error [1054][42s22]: unknown column 'orderdetails.productCode in 'on clause' 的数据类型,不需要打开游标-然后记得在完成操作后将其关闭-等等。隐式游标非常方便,最好学习尽早解决。其次,请注意for rec in (select ...) loop(用我的符号表示)只是一个指针;要从中访问值,您必须引用表列(如rec中所示)-Littlefoot已在其回复中显示了这一点。第三,尝试中的逻辑错误:如果表有多个行,则必须将每个新值的rec初始化为0-在程序开始时,不能只将其初始化为0

话虽如此,这里是一些示例数据,然后是过程及其输出。

rec.num

。 。 。 。

ad