Oracle automatically insert record in multirecord block part 2

元气小坏坏 提交于 2019-12-11 05:57:19

问题


My table looks like this:

+-------------------+
|Name               |
+-------------------+
|Name1              |
|Name2              |
|Name3              |
|Name4              |
|Name1Jr            |
|Name2Jr            |
|Name4Jr            |
+-------------------+

My multirow block looks like:

What I wanted to know is how can I insert a record that has the same name with Jr after I insert a name. For example, I inserted Name2, it will also insert Name2Jr into the multirow block. Like this:

Note: I need to get the value of the automatically inserted data from database.

I tried in WHEN-NEW-RECORD-INSTANCE trigger(the answer of Sir @Littlefoot on my last question):

if :system.trigger_record = 1 and :test.name is null then
    -- do nothing if it is the first record in a form
    null;
else
    duplicate_record;
    if substr(:test.name, -2) = 'Jr' then
        -- you've duplicated a record which already has 'Jr' at the end - don't do it
        :test.name := null;
    else       
        -- concatenate 'Jr' to the duplicated record
        :test.name := :test.name || 'Jr';
    end if;      
end if;  

And now, what I want is to know if there is a way to do it on WHEN-VALIDATE-RECORD trigger. The problem there is that the duplicate_record can't be used in WHEN-VALIDATE-RECORD trigger. How to do it using procedure, function or something? Thanks in advance!


回答1:


DUPLICATE_RECORD is a restricted procedure and you can't use it in WHEN-VALIDATE-RECORD trigger (or any other of the same kind).

As you have to navigate to the next record (if you want to copy it), even if you put that restricted procedure into another PL/SQL program unit, everything will just propagate and - ultimately - raise the same error. So ... you're out of luck.

Even if you wrote a (stored) procedure which would insert that "Jr" row into the database somewhere behind the scene, you'd have to fetch those values to the screen. As EXECUTE_QUERY is the way to do it, and as it is (yet another) restricted procedure, that won't work either.

If you planned to clear data block and fill it manually (by using a loop), you'd have to navigate to next (and next, and next) record with NEXT_RECORD, and that's again a restricted procedure. Besides, if it was a data block (and yes, it is), you'd actually create duplicates for all records once you'd save changes so - either it would fail with unique constraint violation (which is good), or you'd create duplicates (which is worse).

BTW what's wrong with WHEN-NEW-RECORD-INSTANCE? What problems do you have when using it?




回答2:


What We need is

  • a Data Block (BLOCK1) with

    Query Data Source Name is mytable,

    and Number of Records Displayed set to more than 1(let it be 5 as in your case).

  • a Text item named as NAME same as the column name of the table mytable, and Database Item property set to Yes.
  • a Push Button with Number of Records Displayed set to 1,

    Keyboard Navigable and Mouse Navigate properties are set to No

    and has the following code (inside WHEN-BUTTON-PRESSED trigger):

    commit;
    Query_Block1;
    

    Where Query_Block1 is beneath Program Units node with this code :

    PROCEDURE Query_Block1 IS
    BEGIN
      execute_query;
      last_record;
      down;
    END;
    
  • A POST-INSERT trigger beneath BLOCK1 node with code :

       insert into mytable(name) values(:name||'Jr');
    
  • An ON-MESSAGE trigger at the form level with the code (to suppress the messages after commit):

          if    Message_Code in (40400, 40401) then 
            null;
          end if;
    
  • And WHEN-NEW-FORM-INSTANCE trigger with the code :

          Query_Block1;
    


来源:https://stackoverflow.com/questions/50854199/oracle-automatically-insert-record-in-multirecord-block-part-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!