Select 进入 null 或多个值的语句

Select into statement for null or more than one value

我被困在 plsql 中,因为我有 making 功能,如果只有 select 中的值进入 ..

我必须更新 table

如果不来则不来,如果有多个则必须更新和删除所有值。

在下面的函数中,如果第一个 select 变成空值,则不应转到异常处理应该只更新 CUSTOMER table 并且只从 table 3 中删除,如果一个或许多值出现然后对每个值进行所有更新和删除

 create or replace FUNCTION  FUNCTION_NAME (
   from_PARTICIPANT_KEY1   IN NUMBER
)
  RETURN 
IS
   to_participant_key1   NUMBER (11);
BEGIN
   SELECT   to_participant_key
     INTO   to_participant_key1
     FROM   TABLE2
    WHERE   FROM_PARTICIPANT_KEY = from_PARTICIPANT_KEY1;




  UPDATE   CUSTOMERS C
      SET   C.CUSTOMER_STATUS_CD =
               NVL (
                  (SELECT   old_status_cd
                     FROM   TABLE1
                    WHERE   PARTICIPANT_UID = from_PARTICIPANT_KEY1
                            AND participant_cd = 'CUSTOMER'),
                  C.CUSTOMER_STATUS_CD
               )
    WHERE   C.CUSTOMER_UID = from_PARTICIPANT_KEY1;



   UPDATE   subscribers C
      SET   C.STATUS_CD =
               NVL (
                  (SELECT   old_status_cd
                     FROM   TABLE1
                    WHERE   PARTICIPANT_UID = to_participant_key1
                            AND participant_cd = 'SUBSCRIBER'),
                  C.STATUS_CD
               )
    WHERE   C.account_no = to_participant_key1;

   DBMS_OUTPUT.PUT_LINE ('Delete TABLE1 rows');

   DELETE FROM   TABLE3
         WHERE   PARTICIPANT_UID = from_PARTICIPANT_KEY1 AND participant_cd = 'CUSTOMER';

   DELETE FROM   TABLE1
         WHERE   PARTICIPANT_UID = to_PARTICIPANT_KEY1 AND participant_cd = 'SUBSCRIBER';



   COMMIT;



EXCEPTION  -- exception handlers begin
   WHEN NO_DATA_FOUND  THEN  -- handles 'division by zero' error
      dbms_output.put_line('Customer not found ' || from_PARTICIPANT_KEY1);

   WHEN OTHERS THEN  -- handles all other errors
      dbms_output.put_line('Some other kind of error occurred.');


END;

您可以使用 BULK COLLECT INTO 并迭代集合。

首先,您必须声明(或使用一些现有的)集合类型并创建该类型的变量:

  TYPE participant_keys is table of number (11); 
  l_participant_keys participant_keys;

那么,您的查询将更改为:

  SELECT to_participant_key
  BULK COLLECT INTO to_participant_key1
  FROM TABLE2
  WHERE FROM_PARTICIPANT_KEY = from_PARTICIPANT_KEY1;

如果查询不会 return 任何记录,那么您可以用 COUNT:

检查它
if l_participant_keys.COUNT = 0 then
 -- update only CUSTOMER table and only delete from table 3
else
  FOR I IN l_participant_keys.FIRST .. l_participant_keys.LAST LOOP
    --use l_participant_keys(i) do all update and delete for each value
  END LOOP;
end if;