oracle CASE语句不执行任何操作

ghg1uchk  于 2023-10-16  发布在  Oracle
关注(0)|答案(2)|浏览(163)

我有一个触发器和一份案情陈述。在ELSE块的最后一段代码中,我希望触发器不做任何事情,并在前面的CASE语句中的值不匹配时退出。我该怎么做:

create or replace TRIGGER  "CONVEYANCE_REQUEST_T3"  
BEFORE  
insert or update on "CONVEYANCE_REQUEST"  
for each row  
begin  

CASE   
 when :NEW.REGULAR_TRAVEL_MODE = '2 WHEELER' THEN  
 BEGIN  
     CASE  
         when :NEW.WAY_TYPE = 'ONE WAY' THEN   
              SELECT RATE_PER_KM * (SELECT DISTANCE_ONEWAY  FROM     DISTANCE_MASTER WHERE PROJECT_CODE = :NEW.PROJECT_CODE ) * 1 INTO     :NEW.REGULAR_AMOUNT FROM CONVEYANCE_RATE WHERE TRAVEL_MODE = :NEW.REGULAR_TRAVEL_MODE;  
         WHEN :NEW.WAY_TYPE ='TWO WAY' THEN  
              SELECT RATE_PER_KM * (SELECT DISTANCE_ONEWAY FROM DISTANCE_MASTER WHERE PROJECT_CODE = :NEW.PROJECT_CODE ) * 2 INTO :NEW.REGULAR_AMOUNT FROM CONVEYANCE_RATE WHERE TRAVEL_MODE = :NEW.REGULAR_TRAVEL_MODE;  
     END CASE;  
 END;  
 when :NEW.REGULAR_TRAVEL_MODE = '4 WHEELER' THEN  
 BEGIN  
     CASE  
        when :NEW.WAY_TYPE = 'ONE WAY' THEN   
              SELECT RATE_PER_KM * (SELECT DISTANCE_ONEWAY   FROM DISTANCE_MASTER WHERE PROJECT_CODE = :NEW.PROJECT_CODE ) * 1 INTO :NEW.REGULAR_AMOUNT FROM CONVEYANCE_RATE WHERE TRAVEL_MODE = :NEW.REGULAR_TRAVEL_MODE;  
         WHEN :NEW.WAY_TYPE ='TWO WAY' THEN  
              SELECT RATE_PER_KM * (SELECT DISTANCE_ONEWAY  FROM DISTANCE_MASTER WHERE PROJECT_CODE = :NEW.PROJECT_CODE ) * 2 INTO :NEW.REGULAR_AMOUNT FROM CONVEYANCE_RATE WHERE TRAVEL_MODE = :NEW.REGULAR_TRAVEL_MODE;  
     END CASE;  
 END;
 ELSE 
  ****statement to just exit & not do anything*****

 END CASE;  
END;
wztqucjr

wztqucjr1#

这是一个非常复杂的case语句,每次都重复基本相同的select语句。
由于每个语句之间的唯一区别是乘数是1还是2,所以你可以用一个case语句重写整个语句,其中包含一个带有case表达式的sql语句,如下所示:

create or replace trigger  "CONVEYANCE_REQUEST_T3"  
before  
insert or update on "CONVEYANCE_REQUEST"  
for each row  
begin  
  case when rate_per_km in ('2 WHEELER', '4 WHEELER')
            and new.way_type in ('ONE WAY', 'TWO WAY') then
            select rate_per_km
                     * (select distance_oneway from distance_master where project_code = :new.project_code)
                     * case when :new.way_type = 'ONE WAY'
                                 then 1
                            when :new.way_type = 'TWO WAY'
                                 then 2
                            else null
                     end regular_amount
            into   :new.regular_amount
            from   conveyance_rate where travel_mode = :new.regular_travel_mode;
       else null;
  end case; 
end;
/

你甚至可以不使用外部case语句(尽管你必须将rate_per_km in ('2 WHEELER', '4 WHEELER')条件推入case表达式,但这样的话,每次都必须为每一行运行查询,如果插入的大多数行不是2或4轮的话,这可能会降低性能。

qzlgjiam

qzlgjiam2#

If you want to just pass and do nothing with case statement in sql do this ;

当ss.ssal < fs.fsal时选择大小写,然后选择ss.sname
else“”end from ss
-->键入一个空引号,所以它不会返回任何内容。

相关问题