Updating two tables at once

Dear all;
I have a table called using_table with the following field
using_id        life_length          Period
123                   2                   years
124                   1                   months
125                   3                   daysnow i would like a situation whenever the life_length is updated or changed to a new number for the using_table, for instance, the 2 can be changed to 200. If that is the case, this change should be reflected on another table called info_table which has a field column called expiring date. In this table, the expiring date is bascially the mfg date + life_length (period) for that particular id. For example if the mfg date is 01/01/2010 then the expiry date is 01/01/2010 + 2 years(gotten from using_table) = 01/01/2012. Another example is if the mfg date is 01/01/2011 for that id, then expiry date is 01/01/2011 + 1 month (gotten from using_table) = 02/01/2011
see details of the info_table below
info_id              using_id                 mfgdate(MM/dd/YYYY)                Expiry date(MM/DD/YYYY)
A1                       123                          01/01/2010                                    01/01/2012
A2                       124                          01/01/2011                                     02/01/2011Any help will greatly be appreciated. Thank you.

You will need 2 triggers for this. The code i am posting DOES NOT account for a multi-user environment. By this i mean if you have 2 users making concurrent modifications to this data YOU WILL get corrupted data. The only way to avoid that it to lock the tables before allowing access to them (the entire table). This means that only 1 session gets to make changes to this data at a time ... you can read about that here
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:474221407101
As an aside, since you say you are new to Oracle i will share more non-solicited advice with you.
using_id varchar2(50) not null,
info_id varchar2(100)primary key,Don't use character data types to store numeric data. It's bad for a number of reason (all of which you can also read about on AskTom).
Here's the code for the triggers and a simple test case for each.
create or replace trigger i_dont_like_doing_this
after update on using_table
for each row
begin
   update info_table i
      set i.expiry =
         case
            when :new.period = 'years'
            then
               add_months(i.mfgdate, :new.life_length * 12)
            when :new.period = 'months'
            then
               add_months(i.mfgdate, :new.life_length)
            when :new.period = 'days'
            then
               i.mfgdate + :new.life_length
         end
   where
      i.using_id = :new.using_id;
end;
create or replace trigger i_still_dont_like_this
before insert on info_table
for each row
begin
   select
      case
         when u.period = 'years'
         then
            add_months(:new.mfgdate, u.life_length * 12)
         when u.period = 'months'
         then
            add_months(:new.mfgdate, u.life_length)
         when u.period = 'days'
         then
            :new.mfgdate + u.life_length
      end
   into
      :new.expiry   
   from
      using_table u
   where
      u.using_id = :new.using_id;
end;
TUBBY_TUBBZ?insert into info_table values ('1', '125', sysdate, null);
1 row created.
TUBBY_TUBBZ?select * from info_table;
INFO_ID                                                             USING_ID                              MFGDATE           EXPIRY
1                                                                 125                              02-AUG-2010 08 45:51 05-AUG-2010 08 45:51
1 row selected.
TUBBY_TUBBZ?update using_table set life_length = 10 where using_id = '125';
1 row updated.
TUBBY_TUBBZ?select * from info_table;
INFO_ID                                                             USING_ID                              MFGDATE           EXPIRY
1                                                                 125                              02-AUG-2010 08 45:51 12-AUG-2010 08 45:51
1 row selected.
TUBBY_TUBBZ?You mentioned in another post that you will disallow modification of the EXPIRY date column by users. I don't know how you plan to enforce this, but unless you have a VERY good design methodology and lots of experience, i really don't see this happening. I'm not trying to berate you, but i remember back to when i was a beginner in Oracle (10 years ago) and that isn't something i would have been capable of.
10 years later and there's still an exorbitant amount i don't know :(

Similar Messages

  • How To Update two tables at once

    Hi,
    I have to join two tables for an update. Is it possible to update both tables in one update statement? Thank you.
    UPDATE table_a a, table_b b
    SET
    a.name = 'Tom',
    b.city = 'New York'
    WHERE
    a.id = b.id

    Is it possible to update both tables in one update statement? That's not possible.

  • How to insert data into two tables at once using XSJS

    Hello Experts,
    My requirement is to insert data into two tables at once. I have a XS JS app and want to update these tables via xsjs. How can I do this ?
    Is there any mechanism like sql 'transactions' also in Hana ? If yes, how can I call it via xsjs ? If not, what are the ways to populate two different tables at once.
    Please advice. Any help is really appreciated.
    Thanks !

    There is nothing special about inserting into two tables at once in XSJS. Just issue two separate SQL Statements.  Don't execute the COMMIT until after both commands.  That way they are all one transaction.

  • How to use one form to update two tables

    How can I do that? HTMLDB wizard or form on table doesn't give me an option to use more than one table in a form or I don't know about it. I created new process which redirects the form to another page after submitting the form. On the second page I created new process which uses the same variables from the previous form page. This process runs on page load before header but it is just not working right.
    So, what is the proper way to update two tables with the same form fields?

    Hello Vikas,
    "The Automatic Row Fetch and Automatic DML processes are a pair, you can't have one without the other."
    Are you sure about that? I have a page, which populate some of the items from TableA, using manual select statement, and after the user input, save some of it in TableB, using Automatic DML. No ARF in this process and it seems to work just fine. Come to think of it, what about a simple form, populated entirely by the user input, and then being saved to the db, using Automatic DML? No ARF here also.
    For the problem in hand, if you can't have more then one Automatic DML per page, I think that the simplest solution will be to define a pl/sql process, with two INSERT statement to the two different tables.
    Regards,
    Arie.

  • How to update two tables in a single call using JDBC Sender adapter

    Hello All,
    The scenario is, database entries have to be selected from two tables and at the same time those tables have to be udpated with the flag.
    We are using JDBC sender adapter and in Select Query, we are fetching the data by joinin the two tables.
    Update Statemtent: We can only update one table using this statement.
    Is it possible to update two tables using the Update Statement without using Stored Procedures.
    Let me know.
    Regards,
    Sreenivas.

    Hi Sreenivas,
    > Is it possible to update two tables using the Update Statement without using Stored Procedures.
    Yes its possible through join statement
    Check this links
    Update in JDBC Sender adapter for more than one table
    data from 2 tables for jdbc sender adapter
    Regards
    Ramesh

  • Updating two tables at the same time

    Please,
    Does someone have an idea on how to update two tables at the same times?
    Thanks a lot in advance

    I was thinking about a single session and single update command.
    That's why I thougth someone else could have a clue.
    Thank you

  • Can you really update two tables in one SQL statement

    Ok a post in the forum has got me awefully curious:
    Can you update two tables in one update statement i.e something like this
    update table a , b
    set b.<column> = something
    a.<column> = something
    Something to this effect.
    If you are curious to know what post I am talking about it is called "updating a single row"

    No. You can only update one table in one SQL statement.

  • How to update two tables with trigger

    Hi:
    how to update two tables with trigger ?
    I have two tables :
    (1)ASIA
    MI number;
    (2)ASIA_P
    ID number;
    When I insert a new value into the asia.MI ,I also can
    insert the same value into the asia_p.id field.
    I have write a trigger as follows but it does't work.
    create or replace trigger MI_TRG
    before insert on asia
    for each row
    declare
    seq number;
    begin
    select MI_SEQ.Nextval into seq from dual;
    :new.MI:=seq;
    insert into ASIA_PRO(MI_ID)
    values
    (seq);
    end MI_TRG;
    How to realize it ?
    thanks
    zzm

    Why do you say it does not work?

  • Updating two tables through JDBC

    <b>Hai,
    Can anybody please answer the following question?
    How can I update two table in Receiver by using JDBC Adapter at receiver side from single source structure.
    Thanks in advance.</b>

    Hi ,
    Create following structure for inbound data type to insert data in DB using JDBC receiver adapter here dbTableName1 is for first table and dbTableName2 for second table .
    <StatementName2>
    <dbTableName1 action="UPDATE_INSERT">
    <table>realDbTableName</table>
    <access>
    <col1>val1</col1>
    <col2>val2</col2>
    </access>
    <access>
    <col1>val11</col1>
    </access>
    </dbTableName1> 
    <dbTableName2 action="UPDATE_INSERT">
    <table>realDbTableName</table>
    <access>
    <col1>val1</col1>
    <col2>val2</col2>
    </access>
    <access>
    <col1>val11</col1>
    </access>
    </dbTableName2> 
      </StatementName2>
    Thanks ,
    Suvarna
    pls award pts if it helps

  • Using a single Form to Update two tables.

    I would like some assistance please?
    I have an "<strong>Event</strong>" ,"<strong>Item</strong>" and "<strong>Item allocation</strong>" tables . The "Item_allocation" table serves as a composite table joining the "Item " and "Event" table. The relationships are as follows (*1*)Event :(*M*)Item and vice versa. * hence the reason for the third table.
    Attributes are as follows.
    <u>Event</u>
    Eventid(pk)
    Eventname
    Eventdate
    <u>Item</u>
    Itemid(pk)
    Itemname
    Itemcost
    Itemallocation_
    Item_allocationid(pk)
    Eventid(fk)
    Itemid(fk)
    <strong><u>What I'm essentially trying to achieve here is , allow the user to book and event and as part of that process be able to select multiple items.. I need the both "Event " and "Item_allocation" tables to be updated.</u></strong>
    Thank you in advance.

    Hi,
    Try creating the form based on a view and use an INSTEAD OF trigger to create the records in the two tables. Here's an example of this: Report & form on Multiple table
    Andy

  • Updating two table from a single query

    I have two table such as
    user_log(user_id , password)
    and
    user_info(user_id , name , age , address , sex , email , contact)
    If I want to add data from a single form in php then what is the query?

    Hi,
    welcome to the forum..! plesae always post table data and expected result to help forum members help you better.
    From what I understand, all you need is a join on userid (assuming userid is unique/primary key )
    select ul.userid,
             ul.password,
            ui.name,
            ui.age
      from user_log ul,
              user_info ui
      where ul.user_id = ui.user_id
        ---- and any otehr condition for the user that you need.(where clause)In PHP......(based on this link.. http://wiki.oracle.com/page/PHP+Oracle+FAQ
    haven't tested the code.. )
    <?php
    $conn = oci_connect("scott", "tiger", "localhost/XE");
    if (!$conn) {
    $m = oci_error();
    echo $m["message"];
    exit;
    $stid = oci_parse($conn, "select ul.userid,
             ul.password,
            ui.name,
            ui.age
      from user_log ul,
              user_info ui
      where ul.user_id = ui.user_id");
    oci_execute($stid);
    // Query the table
    echo "<table border='1'>";
    while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
    echo "<tr>";
    foreach ($row as $item) {
    echo "<td>".($item!==null?htmlentities($item):" ")."</td>";
    echo "</tr>".PHP_EOL;
    echo "</table>";
    oci_free_statement($stid);
    oci_close($conn);
    ?>Try to provide more details.. if this is not what you are looking for.
    Hope this helps,
    Rajesh.

  • How to update two tables at the same time using jdbc

    Plz will anybody tell me what is the code or query in jdbc by which you insert entry in one table and simultaneously it goes in another table?
    Thanks in advance

    You might be
    able to design a trigger to do what you're talking
    about, but that would be a database thing, not a java
    thingAnother option would be an updatable view (google for usage), but it has limitations and should be used with caution. And yes, its a database thinggy and not java.
    cheers,
    ram.

  • Update two table in receiver jdbc

    Dear all;
    i have one issue sync update as below;
    stmt1.
    one table(TB10) and has key is  MATNR  and  value AAA,   i am trying to update  key value AAA.
    then stmt1_response return to update_count 1.
    stmt2.
    sencond table(TB20) and has key is  MATNR  and  value BBB,   i am trying to update  key value BBB that there is no BBB in table(TB20).
    then stmt2_response return to update_count 0.
    when stmt2_response to update_count 0,  I  want stmt1 to be roll back .  is it possible ?    i realized that  stmt1 is updated although stmt2 is not updated,

    Hi,
    i mad a jdbc lookup,  how do i occur mapping error with result of query execution?
    the following #1 statement  does not work.
    please help ,  how to throw exception on mapping process?
    stmt2.
    for( Iterator rows = resultSet.getRows(); rows.hasNext(); ) {
                Map rowMap = (Map)rows.next();
                result = (String)rowMap.get("MATNR");
    #1    if (!"".equals(result) && result  != null) {
                } else {
                       throw new RuntimeException("no data : " );

  • Recordset - updating 2 tables with 1 recordset using application object update record

    I have a recordset that uses a field from 2 different tables
    with a select statement where clause that joins a userid. I can
    display the field’s data just fine. Now I want to use the
    Application object “update record” so I can modify
    either of the fields. The problem is the Application object
    “update record” only allows you to update one table.
    How does Dreamweaver mx 2004 allow me to update 2 tables with one
    recordset and 1 submit button? Currently using php.
    Example of where:
    Where member.userid = member_detail.userid
    I tried creating the one form with the field from the first
    table and that works just fine. I added the other field from the
    other table into the form but ofcourse there isn’t any code
    that will update the second table so it won’t work.
    My application requires me to update alot of fields between 2
    tables at the same time.
    Does anyone know a way using Dreamweaver mx 2004 to do this?
    I don’t have much php experience.

    jon-rookie wrote:
    > DreamerJim,
    >
    > I am sorry but I don't think you are correct. I just
    can't believe that with
    > all the powers to be at Macromedia and now Adobe can't
    figure out how to update
    > two tables at once. There are millions of db's out there
    that require this. I
    > spent several hours today perusing lots of posts on the
    internet. It seems I
    > am not the only one out there that has asked this
    question. Unfortunately
    > there are no good answers yet to my surprise.
    >
    > I did find a Dreamweaver extension that does exactly
    what I myself and many
    > others want. The problem is it is no longer available
    unless you purchase a
    > bundle of software from Adobe.
    >
    > I have not looked into it in detail so I am not 100%
    sure that is accurate!
    >
    > Still, alot of php programmers do this all the time
    without much trouble. I
    > just want to know if Dreamweaver mx 2004 has the
    capability if a person knows
    > the right steps.
    >
    > Hopefully a Dreamweaver expert will post something to
    let us know for sure.
    > Until then I am stuck.
    >
    Not even CS3 has this built in, you will either have to code
    it yourself
    or buy the extension you have found. Dreamweaver gives you
    basic
    features to help you develop applications, but if you want to
    do
    anything really clever you have to do it yourself.
    One thing to consider is maybe creating an SQL view that is
    can be
    updated. I am pretty sure it exists, then you use the view
    instead of
    the table in the update behaviour. I have never done it
    myself, but I am
    sure it can be done.
    Steve

  • Updating multiples tables

    Hi ,
    i want to update two tables
    ALTER PROCEDURE [dbo].[SP_updateDetails]
    -- Add the parameters for the stored procedure here
    @ServiceID int,
    @DayoftheWeek varchar(50),
    @Date datetime,
    @Services varchar(50),
    @Venue varchar(50),
    @ProjectName varchar(50),
    @StartTime varchar(50),
    @EndTime varchar(50),
    @Breakfast varchar(50),
    @Lunch varchar(50),
    @Dinner varchar(50),
    @Total varchar(50),
    @HoursTotal varchar(50)
    AS
    BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    -- Insert statements for procedure here
    update DetailsTable set Services=@Services,Venue=@Venue,ProjectName=@ProjectName,StartTime=@StartTime,
    EndTime=@EndTime,Breakfast=@Breakfast,Lunch=@Lunch,Dinner=@Dinner,Total=@Total,HoursTotal=@HoursTotal where ServiceID=@ServiceID and DayoftheWeek=@DayoftheWeek
    END
    ALTER PROCEDURE [dbo].[SP_TimeSheetMain_Insert]
    @EmpID int,
    @EmpName varchar(50),
    @StartDate datetime,
    @EndDate datetime,
    @date datetime,
    @day varchar(50),
    @Service nvarchar(max),
    @Venue varchar(50),
    @StartTime varchar(50),
    @EndTime varchar(50),
    @Breakfast varchar(50),
    @Lunch varchar(50),
    @Dinner varchar(50),
    @DailyTotal varchar(50),
    @Total varchar(50)
    AS
    BEGIN
    insert into EmpTimesheet(EmpID,EmpName,StartDate,EndDate,date,day,Service,Venue,StartTime,EndTime,Breakfast,Lunch,Dinner,DailyTotal,Total
    ) values (@EmpID,@EmpName,@StartDate,@EndDate,@date,@day,@Service,@Venue,@StartTime,@EndTime,@Breakfast,@Lunch,@Dinner,@DailyTotal,@Total
    END
    If i update anything in details table i want to update it into emptimesheet table .Do i need to write another update statement in updatedetails storedprocedure or any other ways that i can simply update emptimesheet.

    Are you looking for a permanent way to update table B each time you are updating table A? In other words, any update of table A will lead to an update of Table B? Or sometimes you need to update the tables without dependency?
    For clarification, A trigger is a database element, which execute on a specific event (therefor the name trigger, as the event trigger the action). Once you created a trigger (and it is enable) it will execute on that even every time! In this case Visakh16
    suggested the use of trigger which will execute on any update event. I just want to clarify if that is what you want, then this is the answer, but if you need some times to use update without dependency and sometimes with dependency, then you can use a simple
    query without creating new element on the database, with using an 'OUTPUT INTO'. It will not fit any DDL, but this is very fast and easy way to insert data into table B using an update statement
    on table A.
    Unfortunately you did not post a DDL+DML, and i dont want to do it for all the columns in your query, therefor i will demonstrate using two simple tables. I hope you will be able to implement this on your table. If you dont, then please post DDL+DML.
    Check is this sample code help you:
    use QQ
    go
    CREATE TABLE TableA (
    TableAID INT PRIMARY KEY,
    TableAName NVARCHAR(10)
    CREATE TABLE TableB (
    TableBID int,
    TableBName NVARCHAR(10)
    GO
    select * from TableA
    select * from TableB
    GO
    insert TableA values (1, 'a')
    GO
    update TableA
    set TableAID = 2, TableAName = 'b'
    OUTPUT INSERTED.TableAID,inserted.TableAName INTO TableB
    select * from TableA
    select * from TableB
    GO
    DROP table TableA
    DROP TABLE TableB
    GO
    this is the main Idea:
    update TableA
    set TableAID = 2, TableAName = 'b'
    OUTPUT INSERTED.TableAID,inserted.TableAName INTO TableB
    I hope this is helpful :-)
    [Personal Site] [Blog] [Facebook]

Maybe you are looking for

  • OMF export for Pyramix and ProTools not working correctly

    I am exporting OMF files of very simple timelines (30 second promos) for an audio guy based in another part of the country to do audio post. He is using Pyramix 7.1.10 and ProTools 9.1, while I'm outputting from Premiere 6.0.1 The problem is that whe

  • Does creating Excel,Word,PDF reports & printing in Apex need licensing ?

    I am new to this Apex 4.0 I have installed the Apex 4.0 with Oracle 10g database. Now using Apex if I create any web application, and develop some reports in the web application. 1) Now to do the printing of the reports in Excel,Word,PDF does Apex ne

  • Export to Pages

    I have a Pages document which is a list of items.  I need to alphabetize the list, and am thinking the easiest way to do that is to copy and paste the list into Numbers and sort the list there. However, when I try to copy and paste the now alphabetiz

  • How to use IAS Portal's users in Instant Portal

    Is it possible to give access in Instant Portal pages to IAS Portal's users already existing in OID ? Since I have already the IAS Portal users and I want to give them access to Instant Portal , why not to reference them when defininf Instant Portal

  • Cannot install audio driver because Microsoft bus driver should be installed

    hello! I have a satellite serie A laptop. When i try to install audio drivers it says "Microsoft bus driver should be loaded before installing Realtek High definition audio driver". I've downloaded KB835221 and kb888111xpsp2 uplods and they should fi