How to assign value from insert statement to variable from a trigger

Hi,
I got this really annoying problem and I don't know if I am doing it correctly.
I have a BEFORE INSERT trigger on a table. When someone executes an insert statement I want to grab the value of a column from that statement and assign it to a variable and then do stuff with it. I'm stuck on the assignment.. look below..
CREATE OR REPLACE TRIGGER CARS.geotest2_trigger
BEFORE INSERT ON CARS.GEO_TEST2
FOR EACH ROW
DECLARE
v_chainkey nchar(32);
v_chainkey2 nchar(32);
not_exists EXCEPTION;
BEGIN
:NEW.CHAINKEY := v_chainkey;
SELECT GEO_TEST.CHAINKEY INTO v_chainkey2 FROM GEO_TEST WHERE GEO_TEST.CHAINKEY = v_chainkey;
IF v_chainkey2 = '' or v_chainkey2 is null THEN
RAISE not_exists;
ELSE
INSERT INTO GEO_TEST2 VALUES(:NEW.CHAINKEY, :NEW.BLA, :NEW.FOO);
END IF;
EXCEPTION
WHEN not_exists THEN
RAISE_APPLICATION_ERROR(-20010, 'Chainkey does not exist in parent table GEO_TEST');
END;
I keep getting this error
Error: ORA-04098: trigger 'CARS.GEOTEST2_TRIGGER' is invalid and failed re-validation
SQLState: 42000
ErrorCode: 4098

It isn't assigning because v_chainkey is not at the left hand side of the assignment statement.
test@ORA10G>
test@ORA10G>
test@ORA10G> declare
  2    x  number := 5;
  3    y  number;
  4  begin
  5    x := y; -- does not assign anything to y; assigns NULL to x,
  6            -- because y is NULL at this point
  7            -- so, essentially the value 5 of x is *LOST* now
  8    dbms_output.put_line('x = '||x);
  9  end;
10  /
x =
PL/SQL procedure successfully completed.
test@ORA10G>
test@ORA10G>
test@ORA10G>In any case, here's what you are probably looking for:
test@ORA10G>
test@ORA10G> --
test@ORA10G> drop table geo_test;
drop table geo_test
ERROR at line 1:
ORA-00942: table or view does not exist
test@ORA10G> drop table geo_test2;
drop table geo_test2
ERROR at line 1:
ORA-00942: table or view does not exist
test@ORA10G>
test@ORA10G> create table geo_test (chainkey nchar(32));
Table created.
test@ORA10G> insert into  geo_test (chainkey) values ('a');
1 row created.
test@ORA10G> insert into  geo_test (chainkey) values ('');
1 row created.
test@ORA10G>
test@ORA10G> create table geo_test2 (chainkey nchar(32), bla number(1), foo number(1));
Table created.
test@ORA10G>
test@ORA10G>
test@ORA10G> CREATE OR REPLACE TRIGGER geotest2_trigger
  2  BEFORE INSERT ON GEO_TEST2
  3  FOR EACH ROW
  4  DECLARE
  5    v_chainkey2  nchar(32);
  6    not_exists   EXCEPTION;
  7  BEGIN
  8    SELECT GEO_TEST.CHAINKEY INTO v_chainkey2 FROM GEO_TEST WHERE nvl(GEO_TEST.CHAINKEY,'~') = nvl(:new.chainkey,'~');
  9    IF v_chainkey2 is null THEN
10      RAISE not_exists;
11    END IF;
12  EXCEPTION
13    WHEN not_exists THEN
14      RAISE_APPLICATION_ERROR(-20010, 'Chainkey does not exist in parent table GEO_TEST');
15  END;
16  /
Trigger created.
test@ORA10G>
test@ORA10G> --
test@ORA10G> insert into geo_test2 (chainkey,bla,foo) values ('b',1,1);
insert into geo_test2 (chainkey,bla,foo) values ('b',1,1)
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "TEST.GEOTEST2_TRIGGER", line 5
ORA-04088: error during execution of trigger 'TEST.GEOTEST2_TRIGGER'
test@ORA10G>
test@ORA10G> --
test@ORA10G> insert into geo_test2 (chainkey,bla,foo) values (null,1,1);
insert into geo_test2 (chainkey,bla,foo) values (null,1,1)
ERROR at line 1:
ORA-20010: Chainkey does not exist in parent table GEO_TEST
ORA-06512: at "TEST.GEOTEST2_TRIGGER", line 11
ORA-04088: error during execution of trigger 'TEST.GEOTEST2_TRIGGER'
test@ORA10G>
test@ORA10G> --
test@ORA10G> insert into geo_test2 (chainkey,bla,foo) values ('a',1,1);
1 row created.
test@ORA10G>
test@ORA10G>
test@ORA10G>pratz
I think the sole purpose of that "not_exists" exception is this -
If you try to insert a NULL value for GEO_TEST2.CHAINKEY, then this trigger will throw an error even if at least one NULL value exists in GEO_TEST.CHAINKEY column.
Not sure if that's something that you wanted.
Message was edited by:
pratz

Similar Messages

  • How to retrieve value from insert statement

    hi,
    how can i get a certain value from insert statement and store it in a variable.
    suppose i am inserting emp_no , emp_name , emp_salary to employee table
    and i want to store the emp_name in a variable for other processing,
    how can i accomplish this ? i'm guessing that i have to use trigger,
    but dont know the procedure .
    any help will be greatly appreciated
    thanks

    insert into <table> valiues (....) returning <expression> into <variable>
    You could and should have found this using the SQL Language reference manual
    or
    http://www.morganslibrary.org/reference/insert.html
    Sybrand Bakker
    Senior Oracle DBA

  • How to assign Values to nested table and pass as parameter to procedure?

    How to assign Values to nested table and pass as parameter to procedure?
    Below is the Object and its type
    create or replace type test_object1 as object
    val1 varchar2(50),
    val2 varchar2(50),
         val3 varchar2(50)
    create or replace type test_type1 is table of test_object1;
    create or replace type test_object2 as object
    val1 varchar2(50),
    val2 varchar2(50),
         val3 varchar2(50)
    create or replace type test_type2 is table of test_object2;
    GRANT ALL ON test_object1 TO PUBLIC;
    GRANT ALL ON test_type1 TO PUBLIC;
    GRANT ALL ON test_object2 TO PUBLIC;
    GRANT ALL ON test_type2 TO PUBLIC;
    here is the table made of object type:
    create table test_object_tpe
    sl_num NUMBER,
    description VARCHAR2(100),
    main_val1 test_type1,
    main_val2 test_type2
    NESTED TABLE main_val1 STORE AS tot1
    NESTED TABLE main_val2 STORE AS tot2;
    here is the procedure which inserts values into nested table:
    PROCEDURE INSERT_TEST_DATA(sl_num IN NUMBER,
    description IN VARCHAR2,
    p_main_val1 IN test_type1,
    p_main_val2 IN test_type2
    IS
    BEGIN
    FOR rec in p_main_val1.first..p_main_val1.last
    LOOP
    INSERT INTO xxdl.test_object_tpe
    sl_num,
    description,
    main_val1,
    main_val2
    VALUES
    sl_num
    ,description
    ,test_type1 (test_object1(
    p_main_val1(rec).val1,
                                       p_main_val1(rec).val2,
    p_main_val1(rec).val3
    ,test_type2 (test_object2( p_main_val2(rec).val1,
                        p_main_val2(rec).val2,
                        p_main_val2(rec).val3
    END LOOP;
    commit;
    END INSERT_TEST_DATA;
    here is the anonymoys block which assigns values to the object type and pass values into the procedure:
    set serveroutput on;
    declare
    p_sl_num NUMBER := 1001;
    p_description VARCHAR2(50) := 'Testing Val1';
    inval1 test_type1 := test_type1();
    inval2 test_type2 := test_type2();
    begin
    inval1(1).val1 := 'testx1';
    inval1(1).val2 := 'testx2';
    inval1(1).val3 := 'testx3';
    inval2(1).val1 := 'testy1';
    inval2(1).val2 := 'testy2';
    inval2(1).val3 := 'testy3';
    CSI_PKG.INSERT_TEST_DATA(sl_num => p_sl_num,
    description => p_description,
    p_main_val1 => inval1,
    p_main_val2 => inval2
    end;
    Can anybody correct me.
    Thanks,
    Lavan

    Thanks for posting the DDL and sample code but whenever you post provide your 4 digit Oracle version (result of SELECT * FROM V$VERSION).
    >
    How to assign Values to nested table and pass as parameter to procedure?
    >
    Well you are doing almost everything wrong that could be done wrong.
    Here is code that works to insert data into your table (the procedure isn't even needed).
    declare
    p_sl_num NUMBER := 1001;
    p_description VARCHAR2(50) := 'Testing Val1';
    inval1 test_type1 := test_type1();
    inval2 test_type2 := test_type2();
    begin
    inval1.extend();
    inval1(1) := test_object1('testx1', 'testx2', 'testx3');
    inval2.extend();
    inval2(1) := test_object2('testy1', 'testy2', 'testy3');
    INSERT INTO test_object_tpe
    sl_num,
    description,
    main_val1,
    main_val2
    VALUES
    (p_sl_num, p_description, inval1, inval2);
    commit;
    end;
    /See Example 5-15 Referencing a Nested Table Element in Chap 5 Using PL/SQL Collections and Records in the PL/SQL doc
    http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/collections.htm#CJABEBEA
    1. You don't even need the procedure since all it does is a simple INSERT into the table which you can do directly (see my code above)
    inval1(1).val1 := 'testx1';There is no element one (1) of 'inval1' since you haven't created any elements yet. You need to EXTEND the collection to add an element
    inval1.extend();And then there is an empty element but 'inval1' is a container for objects of type 'test_object1' not for scalars like 'val1', 'val2', and 'val3'.
    So you can't do
    inval1(1).val1 := 'testx1';You have to create an instance of 'test_object1'
    inval1(1) := test_object1('testx1', 'testx2', 'testx3');And so on for the other collection
    You don't need the procedure (as my sample code shows) but once you populate the variables properly it will work.

  • How to improve performance of insert statement

    Hi all,
    How to improve performance of insert statement
    I am inserting 1lac records into table it takes around 20 min..
    Plz help.
    Thanx In Advance.

    I tried :
    SQL> create table test as select * from dba_objects;
    Table created.
    SQL> delete from test;
    3635 rows deleted.
    SQL> commit;
    Commit complete.
    SQL> select count(*) from dba_extents where segment_name='TEST';
    COUNT(*)
    4
    SQL> insert /*+ APPEND */ into test select * from dba_objects;
    3635 rows created.
    SQL> commit;
    Commit complete.
    SQL> select count(*) from dba_extents where segment_name='TEST';
    COUNT(*)
    6
    Cheers, Bhupinder

  • How to assign values to Dynamic VO.

    Hi All,
    I have created a Dynamic VO and i have created 2 attributes(name and age) for it.
    Can i create this dynamic VO in Process form request of one of the CO class?
    Please let me know how to assign values to it......and display it and the page.?
    Thanks,
    Sowmya.

    Mukul
    I am trying to achieve something like below:
    Creating a Dynamic VO in my extended controller's ProcessRequest() method and attaching it to a MessageChoiceBean. In ProcessFormRequest() method based on some condition i have to change my Dynaic VO's whereClauseParam and re-execute it so that it will show some different set of values. Below is the code which i used. But after re-executing it my LOV is still showing old set of values. I have posted a query regd. the same but dint get much help. Kindly let me know, if you have any comments. Also Please find the link to my earlier post.
    Link: Re: Re-executed dynamic VO not showing new values
    * CODE *
    ProcessRequest()
    ViewObject vo = (ViewObject)am.createViewObjectFromQueryStmt("dummyVO","SELECT MEANING AS VALUE FROM HR_LOOKUPS WHERE LOOKUP_TYPE = :1");
    vo.setMaxFetchSize(-1);
    vo.setWhereClauseParam(0,"input1");
    vo.executeQuery();
    OAMessageChoiceBean mesBean=(OAMessageChoiceBean)webBean.findIndexedChild(webBean,"DemoChoiceItem");
    mesBean.setPickListCacheEnabled(false);
    mesBean.setPickListViewObjectDefinitionName("dummyVO ");
    mesBean.setPickListViewUsageName("dummyVO ");
    mesBean.setListDisplayAttribute("VALUE");
    mesBean.setListValueAttribute("VALUE");
    ProcessFormRequest()
    If the event is fired
    S.O.P(....);
    ViewObject vo1 = (ViewObject)am.findViewObject("dummyVO");
    vo1.clearCache();
    vo1.reset();
    vo1.setWhereClause(null);
    vo1.setWhereClauseParams(null);
    vo1.setWhereClauseParam(0,"input2");
    vo1.executeQuery();
    S.O.P(....);
    Thanks

  • How to assigne value in sub record type

    Dear below mention record type database,now i want to assigne value in payr_rec type,in this recrocrd type have one column party_id,but how can assigne value int this field ,
    TYPE group_rec_type IS RECORD(
    group_name VARCHAR2(255),
    group_type VARCHAR2(30),
    created_by_module VARCHAR2(150),
    -- Bug 2467872
    mission_statement VARCHAR2(2000),
    application_id NUMBER,
    party_rec PARTY_REC_TYPE := G_MISS_PARTY_REC
    please guide.

    to get the desired default party_rec attribute value, just assign the "sub-record" attribute defaults as desired; PL/SQL will assign a default (non-null) record as the party_rec value using those attribute defaults:
    create or replace package P_Test_It
    as
         type party_rec_type is record (
              dummy varchar2(1) default 'X'
         type group_rec_type is record (
              group_name VARCHAR2(255),
              group_type VARCHAR2(30),
              created_by_module VARCHAR2(150),
              -- Bug 2467872
              mission_statement VARCHAR2(2000),
              application_id NUMBER,
              party_rec PARTY_REC_TYPE
    end;
    set serveroutput on
    declare
         rec p_test_it.group_rec_type;
    begin
         dbms_output.put_line(rec.party_rec.dummy);
    end;
    X
    PL/SQL procedure successfully completed.Hope it helps.
    Gerard

  • How to assign values for more than one field

    Hi,
    I have written following code
    constants: fieldname(30) value '(SAPMF02D)KNA1-AUFSD'.
    constants: fieldname1(30) value '(SAPMF02D)KNA1-LISFD'.
    constants: fieldname2(30) value '(SAPMF02D)KNA1-FAKSD'.
    field-symbols: <L_FIELD>  TYPE ANY.
    field-symbols: <L_FIELD1> TYPE ANY.
    field-symbols: <L_FIELD2> TYPE ANY.
          Assign (fieldname) to <l_field>.
          <L_FIELD> = 'ZB'. " value according to your requirement
          Assign (fieldname1) to <l_field1>.
          <L_FIELD1> = 'ZB'.
    while debugging <l_field1> is not assinging (fieldname1).
    Im able to assing for (fieldname).
    how to assign value for (fieldname1).
    plz suggest me to assign values for more than one field.
    Regards,
    Brahmaji

    Hello,
    Because there is no field name called LISFD in KNA1. Actually you misspelled the field name.
    It is KNA1-LIFSD

  • How to assign values to server group when you parallelly client-copying

    Hello all,
    I'm now trying to create server group.
    Currently, I use serial process, or default settings, when client-copying.
    But I want to implement this in parallel processes.
    Due to this, I have to create server group and assign a lot of values to something I don't know!
    I want to create a server group, but don't know how to assign values to ones like "Max. requests in queue", "Max. no of logons" and so on.
    Could you please advise me how to allocate values to the resourses shown below;
    Advanced : 1 *I think "1" is the flag for advanced settings
    Max. requests in queue
    Max. no.  of logons
    Max. disp. of own logon
    Max. no. of WPs used: *should be the total of SM50, I guess
    Min. no. of free WPs
    Max. no. of comm. entries
    Max. wait time
    thanks in advance,
    Hozy

    Hi,
      Would you check this [Parallel Processes |http://help.sap.com/saphelp_sm32/helpdata/EN/e8/df959834ac11d586f90000e82013e8/frameset.htm] and [Parallel processes FAQ|http://www.saptechies.com/ccinfo-parallel-processes-faq/] i hope will get some hint.
    Regards
    Mohan Rao

  • How to assign values to JTable using mysql database

    how to assign value to JTable using mysql...

    Search the forum. You use the values of the "ResultSet" to create a "DefaultTableModel" which you then add to the "JTable".
    I'll let you pick the search keywords to use, which I've suggested above. You can also throw in my userid if you want to specifically look for my solution.

  • How can I stop Dreamweaver from inserting spaces after variables?

    How can I stop Dreamweaver from inserting spaces after
    variables?
    Example:
    "...write to you@<!--#echo var="uu" --> and ..."
    is changed by DreamWeaver to
    "...write to you@
    <!--#echo var="uu" -->
    and...>
    When saving or doing other editing, DreamWeaver adds a space
    and a line break before and after the variable. The line break
    isn't hard and doesn't show, but the spaces added before and after
    the variable DO show in browsers. That looks rather stupid and
    incompetent, and requires hand editing on the server after
    uploading.
    Line wrap and code formatting are turned off.
    Is manual correction after uploading the only solution to
    this problem?
    Thanks
    DearWebby

    DearWebby,
    We already discussed this offline, but I want to post the fix
    here for
    the sake of posterity:
    1. Edit > Tag Libraries…
    2. Open HTML Tags folder
    3. Select "directive" folder icon (not really a folder)
    4. Change Line breaks: setting to "No line breaks"
    5. Click OK
    HTH,
    Randy
    > How can I stop Dreamweaver from inserting spaces after
    variables?
    >
    > Example:
    > "...write to you@<!--#echo var="uu" --> and ..."
    > is changed by DreamWeaver to
    > "...write to you@
    > <!--#echo var="uu" -->
    > and...>
    >
    > When saving or doing other editing, DreamWeaver adds a
    space and a line break
    > before and after the variable. The line break isn't hard
    and doesn't show, but
    > the spaces added before and after the variable DO show
    in browsers. That looks
    > rather stupid and incompetent, and requires hand editing
    on the server after
    > uploading.
    >
    > Line wrap and code formatting are turned off.

  • How to assign values to columns in a tabular form

    hello,
    my tabular form allows insert on a table.
    some of the fiels in that table are not null and are hidden from the user.
    before submitting a new row the hidden columns needs to get their values
    from a database function.
    is there a way to do it in a tabular form? and how?
    Thanks,
    Iris.

    HI,
    depends you can use a bunch of different approach, however if you want to use tabular forms note that if you change the qry on wich is based the tforms usually you get an error (if you read around the forum this is because the MULTI PROCESS ROW is built on a table the time you use the wizard).
    If you need this function to 'work' with null value go to
    Tabular Form Element
    --> pl/sql expression or function and call your function in the db.
    Under my opinion if you want to have more control build a tabular form of yours and write your DELETE / UPDATE / INSERT statement and build your tabular form / report using the APEX_ITEM synthax.
    You can use as well a checkbox and loop to build your multirow process (that conditionally process the checked item).
    And if you have a common rules for a specific table i suggest you to manage these rules directly on the that table using triggers.
    hope it helps

  • Generate insert statement using columns from all_tab_cols view

    i am trying to generate a dynamic insert statement using the columns for a table from the all_tab_cols view. if i do a select, i get the output as rows. How do i convert the row out to columns so that i get something like this : INSERT INTO TABLE_NAME (COL1, COL2, COL3,COL4.....) .
    Any help will be appreciated!

    SQL> select * from my_test;
    no rows selected
    SQL> desc my_Test;
    Name                                      Null?    Type
    COL1                                               NUMBER
    COL2                                               NUMBER
    COL3                                               NUMBER
    SQL> declare
      2  cursor c1 is select column_name from all_tab_cols where table_name = 'MY_TEST';
      3  v_sql VARCHAR2(10000) := NULL;
      4  v_cnt NUMBER := 0;
      5  BEGIN
      6  FOR I in C1 LOOP
      7  v_cnt := v_cnt + 1;
      8  v_sql := 'INSERT INTO my_test('||I.column_name||') values('||v_cnt||')';
      9  EXECUTE IMMEDIATE v_sql;
    10  END LOOP;
    11  COMMIT;
    12  end;
    13  /
    PL/SQL procedure successfully completed.
    SQL> select * from my_Test;
          COL1       COL2       COL3
             1
                        2
                                   3
    SQL>

  • Blank line in string value in insert statement causing problems

    I am generating an sql statement (insert) from an xml document using xsl transformation that is later run in an sqlplus session. The issue I am coming across is that one of the tags contains free-form text that can be any number of lines. Most strings are not causing a problem, but when there is a blank line in the string (two or more endline characters together) sqlplus seems to think this signifies the end of a statement. Here is an example that I hope will clarify what is being created.
    INSERT INTO MYTABLE VALUES('THIS WORKS', 0, NULL, ...);
    INSERT INTO MYTABLE VALUES('THIS
    ALSO
    WORKS', 0, NULL, ...);
    INSERT INTO MYTABLE VALUES('THIS
    DOES NOT
    WORK', 0, NULL, ...);
    Anyone seen this problem before or have a quick fix? All I can come up with is to pre-process the xml document (which, by the way, I have no control over the format as it comes from a client) to compress vertical whitespace. (This seems like overkill to me). Any suggestions would be appreciated.
    Thanks,
    Daniel

    I don't know if this will work for u.
    If ur string has more than one end of lines and u don't need them, u can replace them by a single space using
    replace(string,chr(13)||chr(10),' ').
    If this does not solve ur problem then probably ur srting has a special charachter. To remove it
    take the ascii value of the special character and then see what is the corresponding chr value and then use replace function in ur insert statement where u find this special character.
    I too face same kind of problem and this worked for me.

  • Question on Creating Table From Insert statement

    Hi,
    I want to create a table using a DBlink from another database using the create/insert statement. Will it also transfer the constraints or do I have to do it manually afterwards? Thanks.
    Create table T1
    SELECT a,b,c from T2 from DB@dblink;

    As discussed above, the constraints, indexes, triggers are not copied. Only NOT NULL constraints are copied.
    Look at this thread Re: Copying table structure.
    for copying triggers/indexes from source table to destination table.
    Thanks,
    Navaneeth

  • How to assign values to a work area which is a Field Symbol?

    Hello Experts!
    I really need your help! this is the problem:
    I need to assign values to fields into a work area which is a field symbol. The values come from a flat file I uploaded and as I can't know the length I had to build the structure dynamically ( That's why I'm using FS). Each field comes from the file separated by ';', I tried using the SPLIT sentence:
    "SPLIT text AT ';' INTO TABLE <dyn_table>." but the values are assigned vertically into the same field instead of horizontally into each field of the table(field-symbol table).
    I know the name of the field dynamically
    (a TEXT + number) and I know I can't do this
    <dyn_wa>-TEXT1 or field_name = 'TEXT1' <dyn_wa>-(field_name).... ohhh I'm blocked, I don't seem to find the answer, please help!
    Thanks in advance!!
    Frinee

    Now that you have a table with the values, you can move them to the work area.
    data: begin of wa,
          fld1(20) type c,
          fld2(20) type c,
          fld3(20) type c,
    *     and so on
          end of wa.
    data: istr type table of string with header line.
    field-symbols: <fs>.
    split text at ';' into table istr.
    loop at istr.
    assign component sy-tabix of structure wa to <fs>.
    if sy-subrc <> 0.
    exit.
    endif.
    <fs> = istr.
    endloop.
    write:/ wa-fld1, wa-fld2, wa-fld3.
    Now, WA has the values in a horizontal fashion.
    Regards,
    Rich Heilman

Maybe you are looking for