Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2008
    Posts
    3

    Unanswered: Query to update columns from two tables

    Hi
    I want the query to update columns from two tables in Oracle.I have two tables tab1 & tab2.I want to update "tab1_col1" from "tab1" based on "tab1_col2" ,"tab2_col1" AND "tab2_col2" from tab1&tab2 tables.There is some foreign key relation b/w two tables & that column is "col"

    thanks in advance

  2. #2
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    That's what I managed to figure out of your question:
    Code:
    UPDATE tab1 t1 SET
      t1.col1 = (SELECT some column based on "tab1_col2" ,"tab2_col1" AND "tab2_col2"
                 from tab1 & tab2 tables
                 WHERE foreign key relation b/w two tables & that column is "col"
                );
    For something more, please, provide CREATE TABLE and INSERT INTO sample data statements so that we could see what you currently have; also, provide example of how you'd want "tab1.col1" look like after UPDATE.

  3. #3
    Join Date
    Feb 2008
    Posts
    3
    i think now u can come to a clear idea wht my prob is......I am getting errors in this query....

    update tab1
    set tab1.tab1_col1='Y'
    from tab1,tab2
    where tab1.col=tab2.col
    and tab1.tab1_col2 = '1'
    and tab1.tab1_col3 = '123'
    and tab2.tab2_col1 = '18-03-08'
    and tab2.tab2_col2 = '15-03-08'
    ;


    thanks in advance

  4. #4
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    What makes you think that this code *might* work? Did you read the documentation about use of the UPDATE statement? Oracle provides free access to it at http://tahiti.oracle.com so - take a look. Once you learn how to use correct syntax, it will be much easier to write a query.

    Just a suggestion: '18-03-08' "look like" a date, but it is not - it is a string. Always, but ALWAYS use the TO_DATE function in order to make a query work correctly (learn how to do that at the same place - Tahiti).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •