APEX 4.2验证,至少一行必须有一个值

时间:2018-02-02 17:52:00

标签: oracle-sqldeveloper oracle-apex

我有一个表定义具有管理权限的用户。该表有一个标记为ADMINISTRATOR的列。设置顶点表格形式,以便将列显示为复选框。当检查存在时,该值为1.当检查为空时,该值为空。

我遇到的问题是访问此页面的任何管理员都可以向任何人添加或撤消管理员权限。这意味着如果管理员意外或故意撤销包括他自己在内的所有其他管理员的权限,则无法在前端访问该工具。

我想建立一个验证,要求系统至少有一个管理员,如果有人试图在ADMINISTRATOR列中没有1更新表,则抛出错误。

我一直在努力确定哪种验证最有效。

我最近的尝试是:

输入:Function returning a boolean

表达:

declare
   admincount   number(8);
begin

select count(administrator) into admincount from supervisor;

if admincount < 1 then
   return false;
else
   return true;
end if;
end;

当我尝试在Oracle SQL Developer上运行此脚本时,我得到:

Error report -
ORA-06550: line 8, column 4:
PLS-00372: In a procedure, RETURN statement cannot contain an expression
ORA-06550: line 8, column 4:
PL/SQL: Statement ignored
ORA-06550: line 10, column 4:
PLS-00372: In a procedure, RETURN statement cannot contain an expression
ORA-06550: line 10, column 4:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

这段代码有什么问题?我是否将此表达式应用于错误的位置?

2 个答案:

答案 0 :(得分:2)

apex中发生的事情是你的块实际上已转换为函数。

declare
   admincount   number(8);
begin

select count(administrator) into admincount from supervisor;

if admincount < 1 then
   return false;
else
   return true;
end if;
end;

内部更改为具有返回值的此函数。然后引擎调用它。隔离的匿名块不能有return语句,否则您将收到错误。

declare
 ret boolean;
 function x return boolean is
    begin
    declare
       admincount   number(8);
    begin

      select count(administrator) into admincount from supervisor;

      if admincount < 1 then
         return false;
      else
         return true;
      end if;
    end;
  end;

  begin
     ret := x;
  end;

最好将逻辑转换为实际功能

create or replace function is_admin return boolean
as 
   admincount   number(8);
begin

select count(administrator) into admincount from supervisor;

if admincount < 1 then
   return false;
else
   return true;
end if;
end;

然后你可以在sqldev / sqlcl或任何工具中测试这个函数。

然后在APEX中使用的结果表达式将只是

return is_admin;

答案 1 :(得分:1)

它在SQL Developer中不起作用;你必须将BOOLEAN返回到某些东西(另一个PL / SQL程序),然后决定做什么。这是一个例子:

SQL> select * From ts65_supervisor order by id;

        ID ADMINISTRATOR
---------- -------------
         1             1
         2             1
         3
         4
         5             1

SQL> create or replace function f_super return boolean as
  2    admincount number;
  3  begin
  4    select count(administrator) into admincount from ts65_supervisor;
  5
  6    return admincount > 1;
  7  end;
  8  /

Function created.

可以吗? (3位管理员 - 应该是):

SQL> begin
  2    if f_super then
  3       dbms_output.put_line('OK, more than 1 admin');
  4    else
  5       dbms_output.put_line('The last one');
  6    end if;
  7  end;
  8  /
OK, more than 1 admin

PL/SQL procedure successfully completed.

更新后?

SQL> update ts65_supervisor set administrator = null where id < 5;

4 rows updated.

SQL> begin
  2    if f_super then
  3       dbms_output.put_line('OK, more than 1 admin');
  4    else
  5       dbms_output.put_line('The last one');
  6    end if;
  7  end;
  8  /
The last one

PL/SQL procedure successfully completed.

SQL>

我建议您不要在SQL Developer中测试它,而是直接在Apex中测试它 - 它会将您的代码包装到它自己的BEGIN - END块中,并且该函数可能正常工作(注意你的代码和我的代码之间的区别 - 你实际上并不需要IF-THEN-ELSE - 一个带条件的简单RETURN足以让Oracle知道哪一个返回TRUE / FALSE。