为每个插入重置一个变量值

Resetting a variable value for each insert

我有一个PL/SQL函数如下:

create FUNCTION Test
return number
IS cnpParmId NUMBER;
good VARCHAR(1) := 'F';
exist VARCHAR(1) := 'F';
begin
good  := 'F';
exist := 'F';
loop
   select schema1.parm_id_seq.nextval into cnpParmId from dual;
   begin
      select 'T' into good from dual where cnpParmId not in (select PARM_ID from schema1.mytable1);
      exception when NO_DATA_FOUND then good := 'F';
   end;
   exit when good = 'T';
end loop;
return cnpParmId;
end;

现在正在使用此函数尝试将值插入表中:

declare
var1 number := Test;

BEGIN
INSERT INTO schema1.tbl
(PARM_ID, PARM_NAM, PARM_VAL_TXT, PARM_USAGE_TYPE_CD, PARM_USAGE_VAL_TXT, CRTE_TS, CRTE_USER_ID, LST_UPDT_TS, LST_UPDT_USER_ID, SITE_CD)
SELECT var1, 'VEL_INT_TYPE_MI', 'Minutes', 'RTS', NULL, SYSDATE, 'ilogdmgr', SYSDATE, 'ilogdmgr', NULL from dual;

INSERT INTO schema1.tbl
   (PARM_ID, PARM_NAM, PARM_VAL_TXT, PARM_USAGE_TYPE_CD, PARM_USAGE_VAL_TXT, 
    CRTE_TS, CRTE_USER_ID, LST_UPDT_TS, LST_UPDT_USER_ID, SITE_CD)
   select var1, 'Hours', 48, 'RTS', NULL, SYSDATE, 'ilogdmgr', SYSDATE, 'ilogdmgr', NULL from dual;

INSERT INTO schema1.tbl
   (PARM_ID, PARM_NAM, PARM_VAL_TXT, PARM_USAGE_TYPE_CD, PARM_USAGE_VAL_TXT, 
    CRTE_TS, CRTE_USER_ID, LST_UPDT_TS, LST_UPDT_USER_ID, SITE_CD)
   select var1, 'Days', 45, 'RTS', NULL, SYSDATE, 'ilogdmgr', SYSDATE, 'ilogdmgr', NULL from dual;
end;

第一个插入采用从函数返回的值。我希望为第二个插入刷新值。我期待第二个插入将转到函数和 returns 另一个值。但现在这里正在发生这种情况。取相同的值,查询因唯一约束索引而失败。

我会试试select test(),... instead of select var1