Update from another table

I am new in oracle, can anybody help.
I have table price1 and price2, and have same records, only the price is difference.
price1 have field item_num, Group_num, unit_price, case_lot
price2 have field item_num, Group_num, unit_price, case_lot
I want to update unit_price and case_lot from table price1 to table price2 with pisible condition :
1. all record (depend on user)
2. certain group_num (variable, depent on user)
3. certain item_num (variable, depent on user )
Question :
How to update table price2 using price1 in sql and with condition ?
Thank's a lot.

Assuming that field item_num is the primary key of each table:
1. Update price1 maintbl
Set (unit_price, case_lot) =
(Select unit_price, case_lot from price2 where item_num = maintbl.item_num)
Where Exists
(Select 'x' from price2 where item_num = maintbl.item_num)
2. Update price1 maintbl
Set unit_price =
(Select unit_price, case_lot from price2 where item_num = maintbl.item_num)
Where
group_num = whatever ....
And Exists
(Select 'x' from price2 where item_num = maintbl.item_num)
3. Update price1 maintbl
Set unit_price =
(Select unit_price, case_lot from price2 where item_num = maintbl.item_num)
Where
item_num = whatever ....
And Exists
(Select unit_price from price2 where item_num = maintbl.item_num)
There are other (perhaps better) ways of doing this, e.g. updating a view,
but you have not given enough info about your tables. This is a fairly
generic solution.

Similar Messages

  • Updating from another table query

    I have tried a merge query and I think I am not doing this right or for what I want to accomplish, it will not work. Here is the problem:
    I have a lookup table
    create table lkp1(
    lkp_id number,
    lkp_name varchar2(100));
    and have a another table
    create table test(
    test_id number,
    lkp_id number,
    lkp_name varchar2(100));
    I am trying to pull in the lkp_id value from lkp1 table into the test table. I have the lkp_name field in the test table so that I can reference back to lkp1 so that I can pull in the lkp_id value. How would I go about doing this if the lkp_name field in test table has the value duplicated over many rows. For example 5 records in the test table might have the lkp_name of 'ORACLE', and its lkp_id from lkp1 table is 25. How do I populate the lkp_id in the test table with the value from the lkp1 table, without specfiying in the where clasue ...lkp_name = 'ORACLE'. There has to be a way. I tried the merge query, and WOW...I was really off.
    Please help!

    scott@ORA92> -- test data:
    scott@ORA92> select * from lkp1
      2  /
        LKP_ID LKP_NAME
             1 name1
             2 Oracle
    2 rows selected.
    scott@ORA92> select * from test
      2  /
       TEST_ID     LKP_ID LKP_NAME
            10            name1
            20            Oracle
            30            Oracle
    3 rows selected.
    scott@ORA92> -- update:
    scott@ORA92> update test set lkp_id =
      2  (select distinct lkp_id from lkp1 where lkp1.lkp_name = test.lkp_name)
      3  where exists (select lkp_id from lkp1 where lkp1.lkp_name = test.lkp_name)
      4  /
    3 rows updated.
    scott@ORA92> -- results:
    scott@ORA92> select * from test
      2  /
       TEST_ID     LKP_ID LKP_NAME
            10          1 name1
            20          2 Oracle
            30          2 Oracle
    3 rows selected.

  • UPDATING A TABLE WITH SAME INFO FROM ANOTHER TABLE ON THE SAME DB

    0down votefavorite
    I am trying to update a table with info from another table on the same db with same table name. I just want the info to be the same , no primary key or constraint involve just a straight replacement of records and I keep getting errors WITH THE TABLE not
    being recignize. below is my query:
    UPDATE
    VNDFIL
    SET EOBTYP
    =  VNDFIL.EOBTYP, 
    EDI_X12_835_VERSION =  VNDFIL.EDI_X12_835_VERSION
    FROM
    AGERECOVERY
    WHERE
    VNDFIL.EOBTYP
    = VNDFIL.EOBTYP
    AND
    VNDFIL
    .EDI_X12_835_VERSION
    = VNDFIL.EDI_X12_835_VERSION

    Hi rotary,
    If those two same named tables are in the same database then they have to be in different schemas. If you mean they are in the same server instance, then they may be in different databases, besides the "table not being recognized" error,
    anyway you should use the fully qualified table names, that is database.Schema.Table(If across instances, ServerName should be prefixed) to avoid the table unrecognized error.
    Using Identifiers As Object Names
    With the fully qualified names, your update statement can be like below.
    UPDATE
    db1.schema1.VNDFIL
    SET EOBTYP = srcTbl.EOBTYP, EDI_X12_835_VERSION = srcTbl.EDI_X12_835_VERSION
    FROM
    db1.schema2.VNDFIL srcTbl
    WHERE
    db1.schema1.VNDFIL.EOBTYP = srcTbl.VNDFIL.EOBTYP AND
    db1.schema1.VNDFIL.EDI_X12_835_VERSION = srcTbl.VNDFIL.EDI_X12_835_VERSION
    If you have any question, feel free to let me know.
    Eric Zhang
    TechNet Community Support

  • UPDATE SQL string substitution from another table

    Hi - I've come across a tricky situation where I would want to update a column in all the relevant rows of a table with values obtained from another table WITHIN the same column update using string concatenation. im not getting any clean way of doing this.
    The goal is to achieve something like this:
    UPDATE table1 set col1 =
    'LANG_ID=(%s),'||
    '_CHALLENGE_RESPONSE_=(%s),'||
    'CRDN_NAM=(%s),'||
    'CLNT_MODE=(%s),'||
    'PTY_ID=(%s),'||
    'CRDN_VAL=(%s),' ||
    'USER_TZ_COD=(%s),' ||
    'GTS_USER_ID=(%s),'||
    'SP_ID=(%s),' ||
    'SP_ALIAS=(%s),' ||
    'ORIG_CLNT_ID=(%s)'
    where table1.col1 is null
    and to substitute the (%s) with data from another table2*. An example is:
    update table1 set col1=
    'LANG_ID=(select LANG_ID from table2* where USER_ID = (select USER_ID from table1))' ||
    '_CHALLENGE_RESPONSE_=((select CHALLENGERESPONSE_ from table2* where USER_id = (select USER_ID from table1)),' ||
    etc for the rest of the values

    I tried this -although the database shows that the affected rows are updated! but querying the table doesn't show the values in the column!
    select col1 from table1 where col1 is null
    shows back the original content (DESPITE it showing me xxx rows updated!)
    UPDATE table1 t1
    SET col1 = (SELECT 'LANG_ID=('||lang_id||')_CHALLENGE_RESPONSE_=('||
    CHALLENGERESPONSE_|| ...*
    FROM table2 t2
    WHERE t1.user_id = t2.user_id)
    WHERE t1.col1 is null;

  • Looping delete statement where table name is coming from another table

    Hi All,
    We have to write code to delete records from active tables of WDSO.We have 5 DSO  so inspite of writing delete statement for each table we want to put all table names into one table and then loop through it .but we are getting error when we are refering that table field which has active table name.error is :
    "dictionary structure or table is either not active or does not exist "
    As per my understanding in a delete /select /insert /update statement we need to put table name (whose field we are refering ) it can't be replaced by a variable .
    ex: v_table = 'EMPLOYEE' .
    DELETE FROMv_table WHERE EMP_NAME = 'ABDC' .
    is wrong and it must be like
    ex : DELETE FROM EMPLOYEE WHERE EMP_NAME = 'ABDC' .
    but we want to make our code dynamic .
    Can you please suggest a way so that we can read the table names from another table and delete data based on some selection fom those tables .
    I tried variants ,perform etc and even searched FM for the same but not found a solution .Your help will be greatly appreciated .
    Thanks in advance .
    Regards,
    Jaya

    Hi,
    You can change your statement as follows:
    DELETE FROM (v_table) WHERE EMP_NAME = 'ABDC' .
    However, I would not recommend this. There is a standard function module RSAN_ODS_DATA_DELETE which allows selective deletion - that should be a safer way to do this. You can

  • Update from other table

    I want to update one table with values from another table. The code I have written looks like
    update reservations r
    set start_date = (select start_date from sap_update su where su.reservation_id = r.reservationid),
    end_date = (select end_date from sap_update su where su.reservation_id = r.reservationid),
    impressions_rsvd = (select impressions_rsvd from sap_update su where su.reservation_id = r.reservationid),
    sapid = (select sapid from sap_update su where su.reservation_id = r.reservationid),
    state = (select reservation_state from sap_update su where su.reservation_id = r.reservationid);
    which results in me being told that start_date cannot be set to null.
    the intent is to match up records in the reservations table with those in the SAP_UPDATE table (on the reservationid column) and copy the data in corresponding rows from the sap_update to the reservations table.
    In passing, I personally find that the more complex inserts and updates are just as nasty as any select query. But all the books I've come across tend to skip over this aspect rather sharply. Are there any good resources on this? I'm also hampered by having rather more SQL Server experience - which seems to have a different handle on updates.
    Thanks for your help in anticipation!
    Iain

    If your subquery is not returning anyrow (i.e. no matching record in Sap_Update table for the given ReservationID), and you don't want to change existing value then in that case you can change it to:
    update reservations r
    set start_date = (select nvl(su.start_date, r.start_date) from sap_update su
       where su.reservation_id = r.reservationid),
    end_date = (select end_date from sap_update su
       where su.reservation_id = r.reservationid),
    impressions_rsvd = (select impressions_rsvd from sap_update su
       where su.reservation_id = r.reservationid),
    sapid = (select sapid from sap_update su
       where su.reservation_id = r.reservationid),
    state = (select reservation_state from sap_update su
       where su.reservation_id = r.reservationid);In similar fashion you can change your other subqueries too. Another suggestion, you can rewrite your query to:
    update reservations r
    set (start_date, end_date, impressions_rsvd, sapid, state   ) =
    (select nvl(su.start_date, r.start_date), end_date , impressions_rsvd, sapid, state
    from sap_update su
       where su.reservation_id = r.reservationid)
    ;Thanks,
    Dharmesh Patel

  • Breakout table (fill table with matching data from another table)

    Hi
    I've been trying to study old discussions about breakout tables. I feel I'm close, but still no cigar :-)
    In plain english, I'm trying to autocreate rows with data on a table, based on matching values from another table. E.g. have a table to display all rows where type = AssetX
    I have attached a screenshot of my "master table" called Assets:
    I'm looking to prefill Asset name, Total from this table and populate a new table called e.g. Greenhouse
    Where I'd be adding more data (date, income, expense).
    Any help whould be greatly appreciated.
    Thanks!

    Hi,
    Here is a Sample Query.
    Update Emp A
    Set Sal = (Select Sal from emp b where
    a.empno = b.empno)
    where empno in (select empno from emp);
    Regards,
    Ganesh R
    null

  • How to populate a table based on a row selection from another table.

    Hi, i just started to use ADF BC and Faces. Could some one help me or point me a solution on the following scenario .
    By using a search component , a table is being displayed as a search result. If i select any row in the resulted table , i need to populate an another table at the bottom of the same page from another view. These two tables are related by primary key . May i know how to populate a table based on a row selection from another table. Thanks
    ganesh

    I understand your requirement and the tutorial doesn't talk about Association between the views so that you can create a Master-Detail or in DB parlance, a Parent-Child relationship.
    I will assume that we are dealing with two entities here: Department and Employees where a particular Department has many Employees and hence a Parent-Child relationship.
    Firstly, you need to create an Association between the two Entities - Department and Employees. You can do that by right clicking on the model's entity and then associating the two entities with the appropriate key say, DepartmentId.
    Once you have done that, you need to link the two entities in the View section with this Association that you created. Then go to AppModule and make sure that in the Available View Objects: 'EmployeesView' appears under 'DepartmentView' as "EmployeesView via <link you created>". Shuttle the 'DepartmentView' to the right, Data Model and then shuttle
    "EmployeesView via <link you created>" to the right, Data Model under 'DepartmentView'.
    This will then be reflected in your Data Controls. After that, you simply would have to drag this View into your page as a Master-Detail form...and then when you run this page, any row selected in the Master table, would display the data in the Detail table.
    Also, refer to this link: [Master-Detail|http://baigsorcl.blogspot.com/2010/03/creating-master-detail-form-in-adf.html]
    Hope this helps.

  • I can't create table from another table?

    Hi everyone!
    I have a problem that I don't known the reason why?
    I'm using Oracle version 8i and I want to create a table from another table, such as:
         CREATE TABLE a_backup as SELECT * FROM a => It's OK, table a_backup is created.
    But there is only a table that I can't created like that, such as:
         CREATE TABLE b_backup AS SELECT * FROM b;
    When I run over command, SQL Plus is not responding... and clients are can't access to DB or Executing forever
    This is the first time I met this problem.
    Can Anyone help me to resolved it?
    Thanks in advance!

    xi`tin wrote:
    Hi everyone!
    I have a problem that I don't known the reason why?
    I'm using Oracle version 8i and I want to create a table from another table, such as:You realize, of course, that 8i is completely out of support .... Is your OS and hardware just as old as your rdbms software, or is it only the rdbms that your company refuses to upgrade?
         CREATE TABLE a_backup as SELECT * FROM a => It's OK, table a_backup is created.
    But there is only a table that I can't created like that, such as:
         CREATE TABLE b_backup AS SELECT * FROM b;
    When I run over command, SQL Plus is not responding... and clients are can't access to DB or Executing forever
    This is the first time I met this problem.
    Can Anyone help me to resolved it?
    Thanks in advance!

  • INSERTING INTO A TABLE FROM ANOTHER TABLE

    Hi,
    I am having a table called emp in which 5 columns are there, I want to insert some rows from another table called employees, only into three columns of emp table through a sub query.
    Is this scenario is possible, I tried so many queries but it is not working out.
    Can any body help me out.
    The columns in emp table is
    empno, ename, doj, designation, salary, deptno, mgrid
    The columns in employees table is
    empno, name, hiredate, post, salary, commission, deptno, mgr
    I want to insert into empno, ename, doj, designation columns of emp table from the employees table empno, name, hiredate, post columns.
    Ramesh.

    It looks like your EMP table has 7 columns, not 5, and you want to insert into 4 columns, not 3.
    insert into emp
      (empno, ename, doj, designation)
      select empno, name, hiredate, post
        from employees;

  • FETCHING VALUES IN MULTI RECORD BLOCK FROM ANOTHER TABLE USING SELECT STATEMENT.

    Hi,
    I have one multi record block in which i want to fetch values
    (more then one record) from another table using select statement
    IN KEY NEXT ITEM.I am getting following error.
    ORA-01422: exact fetch returns more than requested number of rows
    Thanks in advance.

    In your case I see no reason to use non-database block and to try to populate it from a trigger with a query, instead of using the default forms functionality where you can associate the block and the fields with table, create where clause using bind variables and simply use execute_query() build-in to populate the block. The power of the forms is to use their build-in functionality to interact with the database.
    Also, you can base your block on a query, not on a table and you dynamically change this query using set_block_property() build-in. You can use any dynamic queries (based on different data sources) and you simply need to control the column's data type, the number of the columns and their aliases. Something like creating inline views as a block data source.
    However, you can replace the explicit cursor with implicit one like
    go_block('non_db_block_name');
    first_record();
    FOR v_tab IN (SELECT *
    FROM tab
    WHERE col_name = :variable)
    LOOP
    :non_db_block_name.field1 := v_tab.col1;
    :non_db_block_name.field2 := v_tab.col2;
    next_record();
    END LOOP;

  • SQL select a concatination of entries from another table

    I am trying to select a few columns from one table and then add another column that is a concatination or names from another table that referances it as below. Is this possible to do in one select statement?
    table x1
    ID NAME
    1 Bob
    1 Tom
    2 Bill
    table x2
    ID NAMES
    1 Bob, Tom
    2 Bill
    3

    You mean string aggregation?
    If yes, then it is possible like this, if you are on 10g.
    SQL> create table x1 (id,name)
      2  as
      3  select 1, 'Bob' from dual union all
      4  select 1, 'Tom' from dual union all
      5  select 2, 'Bill' from dual
      6  /
    Tabel is aangemaakt.
    SQL> select id
      2       , rtrim(n,', ') names
      3    from ( select id
      4                , n
      5                , rn
      6             from x1
      7            model
      8                  partition by (id)
      9                  dimension by (row_number() over (partition by id order by null) rn)
    10                  measures (cast(name as varchar2(20)) n)
    11                  rules
    12                  ( n[any] order by rn desc = n[cv()] || ', ' || n[cv()+1]
    13                  )
    14         )
    15   where rn = 1
    16  /
            ID NAMES
             1 Bob, Tom
             2 Bill
    2 rijen zijn geselecteerd.There are several other (slower) techniques possible as well. You can find them by using the search function on this forum.
    Regards,
    Rob.

  • Create table from another table including constraints

    Hi,
    Is there a way to create a table from another table including constraints.
    CREATE TABLE COPY_EMP
    as
    SELECT *
    FROM EMP
    WHERE 1 =2 ;
    This creates the table, but the constraints are not copied over.
    I was reading about DBMS_REDEFINITION - can that be used for this scenario ?
    Thanks!
    Anand

    >
    I tried that, but the constraint names are posing a problem. And unfortunately our constraints are not named in a standard, so am finding it difficult to replace them.
    Was just wondering if there were any simpler approach to this.
    >
    No - there isn't.
    You will have to use new names for the constraints. That usually means extracting the DDL and manually changing the constraint names.

  • Getting description of product from another table

    Hi,
    I am getting the level from the user, and based on that creating an internal table.  I am getting product code (PRODH) and level (STUFE) from one table (T179), and getting product description (VTEXT) from another table (T179T).  I need to create a single internal table (PVS) with all three fields.
    How do I do that?  This may seem like a very simple question, but I have only been teaching myslef ABAP for about one week.
    Once I can prepare this table, I will use a function to display it.
    Regards,
    Al Lal
    Code:
    REPORT  YABHINAV16.
    program to display products at chosen level *
    Tables: T179, T179t.
    types:  begin of hierarchy,
            prodh type t179-prodh,
            vtext type t179t-vtext,
            stufe type t179-stufe,
            end of hierarchy.
    data: pvs type standard table of hierarchy initial size 0.
    select-options level for t179-stufe no intervals.
    Select prodh stufe from T179 into corresponding fields of table pvs where stufe in level.
    select vtext from t179t into corresponding fields of table pvs where prodh = pvs-prodh.
    sort pvs by prodh.

    Hi Just change the code accordingly...
    Tables: T179, T179t.
    types: begin of hierarchy,
    prodh type t179-prodh,
    vtext type t179t-vtext,
    stufe type t179-stufe,
    end of hierarchy.
    types: begin of text,
             prodh type t179t-prodh,
             vtext type  t179t-vtext,
             end fo text.
    data: pvs type standard table of hierarchy initial size 0.
    data: it_text type standard table of text,
            wa_text type text.
    select-options level for t179-stufe no intervals.
    start-of-selection.
    Select prodh stufe from T179 into corresponding fields of table pvs where stufe in level.
    select prodh vtext from t179t into corresponding fields of table text for all entries in pvs where prodh = pvs-prodh.
    end-of-selection.
    sort pvs by prodh.
    sort it_text by prodh.
    loop at pvs.
    read table it_text into wa_text with key prodh = pvs-prodh.
    if sy-subrc eq 0.
    pvs-vtext = wa_text-text.
    endif.
    modify pvs.
    endloop.
    Award Points If useful

  • Default value from another table - sql query

    I'm trying to make the default value of a text item as the result from an sql or pl/sql statement. I keep getting errors when I try to use a pl/sql statement to do this. Has anyone else done this?

    OK, here is what I tried to put in. I have a form and I wanted to add a text field that is derived from another table, such as:
    select question from qa_main where seq=:quest_num
    So, what I want is to have a form where questions on tests are answered, and I want the text of the question to be brought over from the question table. I'm not sure if I'm using the correct syntax, but the ":quest_num" would be a reference to a question number in the answer table that is linked to the sequence number (primary key) in the questions table.
    Of course if there are better ideas, please let me know. :)

Maybe you are looking for

  • Initial time machine backup to Time Capsule failed unexpectedly

    Hi everyone, New MacBook Pro retina, clean install of Mountain Lion, no previous backup to 1 TB third-generation Time Capsule (I deleted the previous .sparsebundle file because it had grown too large). Power Nap enabled in Energy Saver. The initial b

  • 5400 rpm vs 7200 rpm?

    Ok so I have the lacie rugged 5400rpm drive which I thought was 7200 when I bought it but oh well. But I noticed when I do a aja system test I still get like 50mb/sec read / write. Isn't that good enough for video, nevermind the speed?

  • Middle Eastern Version CS6

    Hi all, I'm in the process of upgrading my Photoshop CS5 (yeh, i know...) to CS6. I need the Middle Eastern version because some of my designs include Hebrew. My Q is: at this point Adobe lets me choose Arabic-English BUT no Hebrew!! - Is that the sa

  • Adobe Dreamweaver CS3 Silent Install

    I have followed adobe's instructions on how to perform a silent install with the preserialization built in to the silent install. I recieve an exit code 6 when trying to use the Application Override file that Adobe has provided. Any suggestions?

  • After downloading Bridge CC it will not open.

    Has anyone else experienced this? Any obvious solutions you might share?