简体   繁体   English

PL/SQL 存储过程循环比较值

[英]PL/SQL stored procedure loop compare value

I am using using PLSQL to create an insert stored procedure to insert data to table REGISTER which has 2 input parameters PR_START_FROM and PR_TO_END and I want to compare the two new input parameters the the existing data as shown in this screenshot:我正在使用 PLSQL 创建一个插入存储过程以将数据插入表REGISTER ,该表具有 2 个输入参数PR_START_FROMPR_TO_END ,我想将两个新输入参数与现有数据进行比较,如下面的屏幕截图所示:

在此处输入图片说明

If the new input is not between record one then compare it to record two and if not between record two then compare to record the next record until end of record, if it between all of the record the return false, else return true.如果新输入不在记录一之间,则将其与记录二进行比较,如果不在记录二之间,则比较记录下一条记录,直到记录结束,如果在所有记录之间,则返回false,否则返回true。

This is my code in PLSQL that I tried:这是我在 PLSQL 中尝试过的代码:

create or replace procedure TEST_LOOP(
PR_START_FROM in varchar2,
PR_END in varchar2,
V_CURSOR out SYS_REFCURSOR) is
ROWAMOUT number;
STARTNUM number;
ENDNUM number;
ID number;
begin
  select count(*) into ROWAMOUT from REGISTER;
  for ID in 1..ROWAMOUT loop
    select START_FROM,TO_END into STARTNUM,ENDNUM from REGISTER where REGISTER_ID=ID;
    if (PR_START_FROM between STARTNUM and ENDNUM) and (PR_END between STARTNUM and ENDNUM) then 
      {statement....}
    else
      {statement....}
    end if;
  end loop;
end TEST_LOOP;

Thank you for your help感谢您的帮助

You can check and return true or false string values through a single SQL Select statement by您可以通过单个 SQL Select 语句检查并返回truefalse字符串值

select decode(sign(count(*)),1,'false','true') 
  from register
 where :PR_START_FROM between start_from and to_end 
    or :PR_TO_END between start_from and to_end

whether at least one of the bind variables stay in the range( if so, will return false )是否至少有一个绑定变量留在范围内(如果是,将返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM