PLSQL过程通过从表中获取记录来添加数字

时间:2019-02-27 10:42:24

标签: oracle plsql

PL / SQL过程将值添加到表的每一行中,并且DBMS逐行输出所有结果。

1 个答案:

答案 0 :(得分:0)

像这样吗?

set serveroutput on

declare
  l_what varchar2(10);         --> fail or pass
begin
  for cur_r in (select col1, 
                       col2, 
                       col3,   --> to be compared with
                       col1 + col2 result 
               from your_table)
  loop
    l_what := case when cur_r.col1 + cur_r.col2 = cur_r.col3 then 'pass'
                   else 'fail'
              end;

    dbms_output.put_line(cur_r.col1 ||' + '|| cur_r.col2 ||' = ' || cur_r.result ||' -> '|| l_what);
  end loop;
end;
/

[编辑:对表中的值求和的函数]

基于Scott的架构,此函数将对SAL和COMM值求和:

SQL> select empno, ename, sal, comm from emp where comm is not null;

     EMPNO ENAME             SAL       COMM
---------- ---------- ---------- ----------
      7499 ALLEN            1600        300
      7521 WARD             1250        500
      7654 MARTIN           1250       1400
      7844 TURNER           1500          0

SQL> create or replace function f_sum (par_empno in number)
  2    return number
  3  is
  4    retval number;
  5  begin
  6    select sal + comm
  7      into retval
  8      from emp
  9      where empno = par_empno;
 10    return retval;
 11  end;
 12  /

Function created.

测试:

SQL> select empno, ename, sal, comm, f_sum(empno) result
  2  from emp
  3  where empno = 7499;

     EMPNO ENAME             SAL       COMM     RESULT
---------- ---------- ---------- ---------- ----------
      7499 ALLEN            1600        300       1900

SQL>