Triggers to update one column on 3 tables

hi,
I've three tables:
TABLE TN:
COD_ID.........VARCHAR2(32) primary key
NAME...........VARCHAR2(64) NOT NULL
COD_ID..............NAME
A12.................AT32
A13.................AT33
A14.................AT35
A30.................AT32
TABLE TM:
ID_X..........VARCHAR2(32) PRIMARY KEY
NAME..........VARCHAR2(64)
ID_X..............NAME
A12
A13
A14
A30
TABLE TL:
COD_ID..........VARCHAR2(32) FOREIGN KEY ON TN
ID_X............VARCHAR2(32) FOREIGN KEY ON TM
TL_ID...........VARCHAR2(32) PRIMARY KEY
NAME............VARCHAR2(64)
I'd like to create triggers (or procedure) that update automatically the value NAME in the tables TM and TL.
for example:
If I insert into TN a new COD_ID (A39) and a new NAME (AT38), when I insert a new ID_X in tble TM, I'd like
to have the value AT38 in NAME of TM, and when I insert a new record in table TL I'd like
to have the value AT38 in NAME of TL.
Also when I update NAME of TN.
I tried with these triggers:
CREATE OR REPLACE TRIGGER TN_PR_BEFORE_UPD
BEFORE UPDATE OF NAME ON TL
FOR EACH ROW
DECLARE
tmpVar NUMBER;
BEGIN
update TL set NAME=:new.NAME where TL.COD_id=:new.COD_id;
UPDATE TM SET NAME=:NEW.NAME WHERE ID_X=:NEW.COD_ID;
END TN_PR_BEFORE_UPD;
CREATE OR REPLACE TRIGGER TN_COD_ID
BEFORE UPDATE OF COD_ID ON TL
FOR EACH ROW
BEGIN
SELECT NAME INTO :NEW.NAME FROM TN WHERE TN.COD_ID=:NEW.COD_ID;
END TN_COD_ID;
CREATE OR REPLACE TRIGGER TL_pr
BEFORE UPDATE OF NAME ON TL
BEGIN
UPDATE TM SET NAME=:NEW.NAME WHERE ID_X=:NEW.ID_X AND :NEW.ID_X IS NOT NULL;
END TL_pr;
CREATE OR REPLACE TRIGGER TL_PR_BEFORE_INSERT
before INSERT ON TL
FOR EACH ROW
DECLARE
tmpVar varchar2(64);
BEGIN
SELECT NAME
into tmpVar
FROM TN
WHERE COD_ID=:NEW.COD_ID;
END TL_PR_BEFORE_INSERT;
when I update TN triggers run CORRECTLY but when I insert a new record in TM, TN, TL I don't see a new NAME.
What I wrong??
Thanks
Raf

Use when updating then insert into table_name
and
when inserting then insert into table_name
clauses in your Triggers to do this

Similar Messages

  • Need to update one column of all table in that particular schema

    hi all,
    i am using db10g.
    my task is to update one particular column's value of all the table in that schema.
    to acheive the above
    do i have to write update statement for each table or is there any way to do it in bulk?
    i hope my question make sense.
    Thanks..

    Hi karthick,
    I want to know something which is not related to this thread .
    How to include formatted post while replying to thread .
    I know using will be used  format  code before posting  . But how can we  do  it for posted message .
    I hope you understand my question .
    Thanks in advance .                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Updating a column of a table

    Hi all
    I have a problem in updating one column in the target table(say table A) from a column in the source table (say table B).
    All the columns in table A has been populated. Just one column should be populated. My source is a SQL object that contains a complex sql query that returns three columns date_from, date_to and ID.
    In the table A, I have the same date_from and date_to and need to get the ID from the SQL object and set it to the corresponding ID in the table A.
    I tried many ways to update the table, but it I couldn't ;(
    I should read from a table and update it based on a look-up function mapped to the column I need to update.
    I want to map the ID colmn from table A to a look-up ext function that can get the column from the table B. However I don't know how to use the Data Services objects to just update 1 column. If you could give me a consequence of the DS objects to be update a table based on a look-up , I would be so grateful.
    Thanks.

    Your dataflow should look like this :
    1) Source = the SQL transform with date_from, date_to and ID as columns.
    2) Next add a query (copy all columns from schema in to schema out) and set date_from and date_to as keys. Also make sure column names exactly match the column names in your target table, if not rename them in this query.
    3) Add a Map_Operation transform that maps all "normal" operation codes to "update"
    4) Finally add your target table and in the properties/options set "use input keys" to yes (or checked).
    This will generate UPDATE statements for all columns used in the query with a WHERE clause based on the keys defined in the query (date_from, date_to).
    Ben.

  • Update one column while getting the value in another column

    Is it possible to in one single SQL statement update two columns in a table while at the same time obtaining the value of another column, on the same row, in the same table, and independently (that is, the update of the columns has nothing to do with the data that I want from another column)*?* Of course, I can do this in two operations, one being a "select" and the other one being an "update", but since I am in the same table and even on the same row, is it really necessary to make TWO database calls? That's why I want to combine the the two SQL-statements, for reasons of presumed effiency.

    jsun wrote:
    Is it possible to in one single SQL statement update two columns in a table while at the same time obtaining the value of another column, on the same row, in the same table, and independently (that is, the update of the columns has nothing to do with the data that I want from another column)*?* Of course, I can do this in two operations, one being a "select" and the other one being an "update", but since I am in the same table and even on the same row, is it really necessary to make TWO database calls? That's why I want to combine the the two SQL-statements, for reasons of presumed effiency.Two statements != two database calls.
    At least not in terms of SQL.
    JDBC requires a 'statement' but in SQL (depending on the data source) that can include multiple statements. An obvious example of this is a stored proc but there are other ways as well.

  • How to get one column of ALV table as dropdown by key.

    Hi experts,
                  How can I get one column of ALV table as dropdown and editable. If  user wants to change that column value he can just select from that dropdown and click on update button. Can I provide tool tip to that column as " Select from drop down to change the status "?
      Please Help.
    Thanks,
      Pratibha

    You just need to change the cell editor of that column in ALV.
    So first get access to the alv model object (adjusting the code below for your ALV Component Usage name - mine was ALV_ADV):
    DATA: l_ref_cmp_usage TYPE REF TO if_wd_component_usage.
      l_ref_cmp_usage =   wd_this->wd_cpuse_alv_adv( ).
      IF l_ref_cmp_usage->has_active_component( ) IS INITIAL.
        l_ref_cmp_usage->create_component( ).
      ENDIF.
      DATA l_salv_wd_table TYPE REF TO iwci_salv_wd_table.
      l_salv_wd_table = wd_this->wd_cpifc_alv_adv( ).
      DATA l_table TYPE REF TO cl_salv_wd_config_table.
      l_table = l_salv_wd_table->get_model( ).
    Then access the column object you want to change:
    DATA l_column TYPE REF TO cl_salv_wd_column.
      l_column = l_table->if_salv_wd_column_settings~get_column( 'REGION' ).
    Then create the cell editor for DDLB and set it as the new cell editor for this column:
    DATA ddlb TYPE REF TO cl_salv_wd_uie_dropdown_by_key.
      create object ddlb
        exporting
          selected_key_fieldname = 'REGION'.
      ddlb->set_tooltip( `Select from drop down to change the status` ).
      l_column->set_cell_editor( ddlb ).

  • After adding one column in a table can't get the header line

    hi all,
    i have make a SMART FORM which was working perfectly.Now the requirement of user is to add one column in my table at smart form which i did and after modification i execute it it give me data as well but issue which i'm facing is that the TABLE at smart forms in which i have add one field is not displaying the HEADER LINE which i have define with SELECT PATTREN option.
    Thanks & Regards,
    sappk25

    Hi,
    Have you created the Header Text? If yes, then might be the case that your smartforms
    table which was already created is with Multiple Line. Check weather you have added
    customer field in Header Line or Not?
    Regards,
    SUjeet

  • How to update two columns in two tables?

    hi friends
    I have two tables linked to each other through SaleNo and SaleDT. their structure as below
    Sales Table============
    SaleNo int PK auto increment
    SaleDT Datetime PK
    Qnty decimal
    Units decimal
    Invoices table
    =================InvoiceNo int PK Auto incremented
    InvoiceDT Datetime
    SaleNo int FK
    SaleDT Datetime FK
    Note that SaleDT column is NOT assigned with getDate() expression.
    1. what I need to do is update the SaleDT column of Sales table and Invoices table with the value '2013-01-31 10:31:55.813', how do I do this without manually breaking the link between the tables?
    2. Assume SaleNos 15 to 27 needs update the SaleDT to '2013-06-12 10:31:55.813', how do I do this complex operation where I I have update SaleDt column of two table of SaleNo range from 15 to 27?
    thanks
    I use Visual studio 2012 Ultimate and SQL server 2008 developer edition!

    Why not below? May be you will not be able to change your design now, but just want to share.
    Sales Table============
    SALEID int PK autoincrement
    SaleNo int
    SaleDT Datetime
    Qnty decimal
    Units decimalUNIQUE (Saleno,SaleDT)
    Invoices table
    =================InvoiceNo int PK Auto incremented
    InvoiceDT Datetime
    SALEID int FK

  • How to update multiple columns from different tables using cursor.

    Hi,
    i have two tables test1 and test2. i want to udpate the column(DEPT_DSCR) of both the tables TEST1 and TEST2 using select for update and current of...using cursor.
    I have a code written as follows :
    DECLARE
    v_mydept1 TEST1.DEPT_CD%TYPE;
    v_mydept2 TEST2.DEPT_CD%TYPE;
    CURSOR C1 IS SELECT TEST1.DEPT_CD,TEST2.DEPT_CD FROM TEST1,TEST2 WHERE TEST1.DEPT_CD = TEST2.DEPT_CD AND TEST1.DEPT_CD = 'AA' FOR UPDATE OF TEST1.DEPT_DSCR,TEST2.DEPT_DSCR;
    BEGIN
    OPEN C1;
         LOOP
              FETCH C1 INTO v_mydept1,v_mydept2;
              EXIT WHEN C1%NOTFOUND;
              UPDATE TEST2 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
              UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
         END LOOP;
         COMMIT;
    END;
    The above code when run says that it runs successfully. But it does not updates the desired columns[DEPT_DSCR].
    It only works when we want to update single or multiple columns of same table...i.e. by providing these columns after "FOR UPDATE OF"
    I am not sure what is the exact problem when we want to update multiple columns of different tables.
    Can anyone help me on this ?

    oops my mistake.....typo mistake...it should have been as follows --
    UPDATE TEST1 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
    UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
    Now here is the upated PL/SQL code where we are trying to update columns of different tables --
    DECLARE
    v_mydept1 TEST1.DEPT_CD%TYPE;
    v_mydept2 TEST2.DEPT_CD%TYPE;
    CURSOR C1 IS SELECT TEST1.DEPT_CD,TEST2.DEPT_CD FROM TEST1,TEST2 WHERE TEST1.DEPT_CD = TEST2.DEPT_CD AND TEST1.DEPT_CD = 'AA' FOR UPDATE OF TEST1.DEPT_DSCR,TEST2.DEPT_DSCR;
    BEGIN
    OPEN C1;
    LOOP
    FETCH C1 INTO v_mydept1,v_mydept2;
    EXIT WHEN C1%NOTFOUND;
    UPDATE TEST1 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
    UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
    END LOOP;
    COMMIT;
    END;
    Please let us know why it is not updating by using using CURRENT OF

  • Updating a column from other table

    Hi all,
    I have a simple update query problem. I have four tables
    activist(activist_id,first_name,last_name,c_state),
    membership(activist_id,g_n_id),
    group_network(g_n_id,g_n_type_id),
    school_grop_det(g_n_id,state). For some records in activist table the c_state column is null, i want to update that column with state column of school_group_det table.
    Here is the query for the states which are null
    select distinct a.activist_id,a.first_name,a.last_name,
    a.c_state,sd.state from activist a,membership m,
    group_network g,school_group_det sd where
    a.activist_id=m.activist_id and g.g_n_id=m.g_n_id and
    g.g_n_id=sd.g_N_id and a.c_state is null and g.g_n_type_id='1001'
    order by a.activist_id
    I got the activist_id,first_name,last_name,c_state from activist and state from school_group_det. now i as i told you want to update the c_state with state column of school_group_Det table.
    Pleae any one help me
    Thanks
    Srinivas

    For ur query the reply whay u have got is correct one. For further quries similar to one u had u can download a tool named TOAD (Tools for Oracle Application Developer) from quest site (www.quest.com). Install it a proceed.
    Do let me know how and what u feek about TOAD.

  • Updating the column of a table

    Hello Gurur,
    I have two tables they are
    Test & emp_op
    Desc  empop
    - empid
    -id_num
    -node_num
    _Desc Test
    -id_num
    -node_num
    -name
    -addr
    -empid
    Now i m trying to update the column empid in test table, copying as it is from empid column of emp_op table . Below is the query i have wriiten to update
    update test e
    set e.empid = (select t.empid from emp_op t
    where t.id_num = e.id_num and
    t.node_num = e.node_num );
    But it gives me an error ORA-01427: single-row subquery returns more than one row. any idea why ??
    if not, is there is any way i can accomplish this task in a different way ??
    Thank you so much. I appriciate it!!

    Hi,
    That means some row (maybe more than one) in test matches two or more rows in emp_op.
    What do you want to do in that case? Post some sample data (from both tables, relevant columns only) and the results you want to see in test after the UPDATE.
    If you know that all the matching rows in emp_op will have the same empid, you can just change SELECT in the sub-query to SELECT DISTINCT .

  • SQL update one column from another column

    I'm looking for a way to update a SQL column with a portion
    of info from another column in the same table.
    example of a sql command
    UPDATE table1
    SET table1.columnname1 = table1.columnname2
    FROM table
    WHERE blah blah blah
    Here's the thing... I only need a portion of the data found
    in the source column. I'm not sure how I would do this then.
    for example, the database has countries and states combined
    into one column like this 'US-DC', 'US-CA', US-FL', etc. I want to
    separate these into two columns, a country column and a state
    column.... and I dont want to go though all the results and do this
    line by line. How would I write the SQL command so that it puts
    just the country in the country column, and puts just the state in
    the state column, and it omits the dash all together.
    any ideas?

    It depends. Are you using PHP and mySQL or ASP and SQL?
    If you are using mysql and PHP you could use the following
    code to loop through all records in your datatbase.
    $sql="SELECT * FROM tablename"; //SET tablename to the name
    of your database table.
    $result=mysql_query($sql)or die(mysql_error(). " - $sql");
    for($x = 0; $row=mysql_fetch_assoc($result); $x++){
    //set up the variables from the table
    $ID=$row['ID']; //this is the primary key
    $var1=$row['contrystatecolumn']; //this is the name of the
    column which holds the country-state
    $var2=explode("-", $var1); //now we explode the string to get
    the country and the state seperated and place the results into an
    array
    $country=$var2[0]; //this is the first piece of the exploded
    string in the array
    $state=$var2[1]; //this is the second piece of the exploded
    string in the array
    //now we update the database record with the new information
    //remember to replace the tablename with your actual table
    name and the columns country and state with the acutal names of
    your columns in your table
    $sql2="UPDATE tablename SET country='$country'.
    state='$state' WHERE ID='$ID'";
    $result2=mysql_query($sql2)or die(mysql_error() . " -
    $sql2");
    This code will loop through every record in your table and do
    what you want very quickly.
    Hope this helps
    Stan Forrest
    Senior Web Developer
    ATG Internet

  • Need logic to update Varchar column in a table

    Hi,
    Could you give me logic for below.
    I have a table xx_dbc where one of the column Attribute1 is varchar2(240).
    Attribute1 has values like 
    ==================
    (AIN,PUP,GRI,NINE)
    (AIN,PUP)
    (AIN)
    (GRI,NINE)
    I have to update this column for every record with values AIN=10 , PUP=20 , GRI=30 , NINE=40
    Columns should be updated like below
    (10,20,30,40)
    (10,20)
    (10)
    (30,40)
    Thanks.

    Hi,
    Here's one way:
    WITH  replacements  AS
        SELECT  'AIN' AS old_str, '10' AS new_str  FROM dual  UNION ALL
        SELECT  'PUP',            '20'             FROM dual  UNION ALL
        SELECT  'GRI',            '30'             FROM dual  UNION ALL
        SELECT  'NINE',           '40'             FROM dual
    ,    normalized_data  AS
        SELECT  p_key
        ,       LEVEL               AS sort_key
        ,       REGEXP_SUBSTR ( attribute1
                              , '[^(,)]+'
                              , 1
                              , LEVEL
                              )     AS str
        FROM    xx_dbc
    --  WHERE   ...     -- If you need any filtering, put it here
        CONNECT BY   LEVEL              <= 1 + REGEXP_COUNT (attribute1, ',')
                AND  PRIOR p_key        = p_key
                AND  PRIOR SYS_GUID ()  IS NOT NULL
    SELECT    '(' || LISTAGG ( NVL (r.new_str, n.str)
                             ) WITHIN GROUP (ORDER BY sort_key)
                  || ')'  AS  new_attribute1
    ,         n.p_key                         -- If wanted
    FROM             normalized_data  n
    LEFT OUTER JOIN  replacements     r  ON  r.old_str  = n.str
    GROUP BY  n.p_key
    ORDER BY  n.p_key
    Like everything else, it depends on your Oracle version.
    In the query above, p_key can be any unique key from xx_dbc, including attribute1 or ROWID.
    Relational databases, like Oracle, work best when each column of each row contains only 1 piece of information, not a delimited list of any number of pieces.  This is so fundamental to table design that it's called First Normal Form.  Most of the work in this problem involves converting your denormalized data into First Normal Form, and then converting it back again.  This problem, like many others, would be much simpler and more efficient if your table was in First Normal Form.
    The query above assumes the replacement strings (such as 'AIN' and '10') are not already in a table.  If they are, or if they can be derived from a table, then you don't need the replacements sub-query; use your real table instead.

  • Update one column in Query only block

    Hi,
    I have a question regarding multi-record, multi-columns form block.
    A block has 100 columns (column1 to column100).
    We have two separate version of same form. In query only mode, update_allowed property of all the blocks in the form is False.
    But in query only form, one specifc column should be updateable.
    How can use Custom.pll to achive this functionality ?
    I tried following steps
    1. In when-new-item instance of column 54, wrote set_item_property and set_block_property
    2. Update allowed for block and specific item is True
    I know that Block property will override the property of items.
    Now all the items are updateable in query only form. But i want only one column updateable.
    Appreciate you valuable input on this.
    Thanks

    This is an eBusiness-related question, you should post it in the eBusiness-forum. From the "pure" forms point of view, set the Block as updateable and set all item except the one you want to be updateable to UPDATE_ALLOWED =PROPERTY_FALSE

  • How do I add more than one column to a table using SQL?

    Hi
    I need to add 3 columns to a table using SQL
    the syntax
    "ALTER TABLE TEST ADD COLUMN newcol1 float";
    works fine - for adding one coumn only.
    For multiple columns I tried various permutations along the lines of
    "ALTER TABLE TEST ADD (COLUMN newcol01 float, COLUMN new2 float,COLUMN new3 float)";
    "ALTER TABLE TIPSTEST ADD COLUMN new1 float"
    "ALTER TABLE TIPSTEST ADD COLUMN new2 float"
    "ALTER TABLE TIPSTEST ADD COLUMN new3 float"
    etc., but this doesn't work.
    From a web search it sounds like SQL can only add one column at a time.
    I have a workaround : create intermediate temporary tables , copying data and adding
    one column at each stage. It seems a fairly awkward way of programming though.
    Am I missing something simple : is there a way to add multiple columns in one go?
    Thanks

    OK : solved an underlying problem with this one myself
    for the code
    String createString;
    createString =  "select COFFEES.* INTO NEWCOFFEES FROM COFFEES"; // example
              Statement stmt;
              try {
                   stmt = a_Globals.database1Connection.createStatement();
                          stmt.executeUpdate(createString);
                   stmt.close();
                   a_Globals.database1Connection.close();
              } catch(SQLException ex) {
                   System.err.println("SQLException: " + ex.getMessage());
              }                    commenting out the line:
    a_Globals.database1Connection.close();
    Allowed the subsequent SQL statement(s) to work OK. Looks like this was the cause of several
    difficulties, preventing me doing several SQL instructions in turn.
    Thanks for the responses.
    Mike2z

  • Triggers to Update rows in the same table

    Hi,
    I have a table called as "Cal" which has the following fields
    CALID
    Description
    RecurringID
    There will be many records with the same recurring id and If the Description field is Updated, then I would like to Update the same description to all the records with the same recurring id (same table). I have tried it with triggers and the I get the ORA-00036 Maximum number of Recursive SQL reached error.
    I have a a row level trigger which captures the change and I use the statment level trigger to update the description field (same table) and I get the error mentioned above.
    Thx in advance
    Ravi

    I have three kind of suggestion.
    (1) Use session variables in a package as flag for prevention from infinitely recursive calling.
    (2) Use temporary tables for similar reason.
    (3) Use instead-of-trigger (need to create view).

Maybe you are looking for

  • Non english characters in DN cannot be retrieved

    We are using Netscape directory server 4, protocal V3. We have a problem related to non-english characters appearing in RDN. We publish to Ldap entries using the values from database. For example, we have pubulished an entry to Ldap, based on DB valu

  • Web Photo Gallery help

    I have a couple of questions about using Bridge to make web photo galleries. - is there a way to make the galleries so the pictures retain the same file names as the original pictures? Here's a sample gallery I made; http://www.bobcoulter.com/ps5/gal

  • FM IN LANG PROB

    HI IAM USING FM READ_TEXT_INLINE. IN THAT I ENTER DATA THEN I WILL GIVE ERROR MESSAGE.ITS EN IS NOT FOUND. I ENTER ANOTHER DATA ITS WORKING. WHAT CAN I DO

  • ALV multiple lists: Set tab focus

    Hi, I have a report that is producing 4 ALV lists on a screen, each of which is output in its own subscreen. However, what I need to be able to do is set the tab focus based on the data selected. ie. If no data is found to display in subscreen 1, I w

  • Passing LrPhotos in arrays

    I know in the table-based world of Lua this is probably klutzy, but hear me out. In one part of my code I am creating an Array of Arrays containing information about some LrPhotos as well (for good measure) as the actual LrPhotos, and I'm putting it