Update a column in a table using the values in another table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    Update a column in a table using the values in another table

    There are two tables in my database
    1. Table_A
    2. Table_B


    Structure for Table_A
    Code:
     A_ID - A_code(FK to B_ID) 
    1001 - Null
    1002 - Null
    1003 - Null
    Structure for Table_B
    Code:
     B_ID - B_A_ID (FK to A_ID) 
    501 - 1001 
    502 - 1003 
    503 - 1002
    I need to update A_code column with B_ID with the according to the relation ship.this might be work for single recored.

    [CODE=sql]
    UPDATE Table_A SET A_code = (select
    Table_B.B_ID
    from Table_B
    Inner join Table_A on
    Table_A.A_ID=Ta ble_B.B_A_ID
    Where Table_B.B_A_ID = 1001)
    Where A_ID = 1001[/CODE]

    Then how to update column A_code at once?
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    Originally posted by ajaxrand
    There are two tables in my database
    1. Table_A
    2. Table_B


    Structure for Table_A
    Code:
     A_ID - A_code(FK to B_ID) 
    1001 - Null
    1002 - Null
    1003 - Null
    Structure for Table_B
    Code:
     B_ID - B_A_ID (FK to A_ID) 
    501 - 1001 
    502 - 1003 
    503 - 1002
    I need to update A_code column with B_ID with the according to the relation ship.this might be work for single recored.

    [CODE=sql]
    UPDATE Table_A SET A_code = (select
    Table_B.B_ID
    from Table_B
    Inner join Table_A on
    Table_A.A_ID=Ta ble_B.B_A_ID
    Where Table_B.B_A_ID = 1001)
    Where A_ID = 1001[/CODE]

    Then how to update column A_code at once?

    Code:
    UPDATE Table1 INNER JOIN Table2 ON Table1.A_ID = Table2.B_A_ID SET Table1.A_COde = [Table2].[B_ID]


    concider Table_A as Table1
    Table_B as Table2

    i think this will help you

    Comment

    • hariharanmca
      Top Contributor
      • Dec 2006
      • 1977

      #3
      Originally posted by hariharanmca
      Code:
      UPDATE Table1 INNER JOIN Table2 ON Table1.A_ID = Table2.B_A_ID SET Table1.A_COde = [Table2].[B_ID]


      concider Table_A as Table1
      Table_B as Table2

      i think this will help you

      Code:
      UPDATE Table1 INNER JOIN Table2 ON Table1.A_ID = Table2.B_A_ID SET Table1.A_COde = Table2.B_ID
      WHERE (((Table1.A_ID) Like '1001'))
      this will work for 1 record, which is 1001

      if Table1.A_ID is text type then WHERE (((Table1.A_ID) Like '1001'))
      if Table1.A_ID is Number type then WHERE (((Table1.A_ID) = 1001))

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        I create a sample table with this Queries.But failed when try to run the update.
        [CODE=sql]
        Create Table Table_1(
        ID int primary key Identity (1001,1),
        Code varchar(10) null
        )
        Go

        insert into Table_1(Code)va lues(null)
        insert into Table_1(Code)va lues(null)
        insert into Table_1(Code)va lues(null)

        Go
        Create Table Table_2(
        ID int primary key Identity (501,1),
        FK_ID int not null,
        FOREIGN KEY (FK_ID) REFERENCES Table_1(Id)
        )
        Go
        insert into table_2(FK_ID)v alues(1001)
        insert into table_2(FK_ID)v alues(1002)
        insert into table_2(FK_ID)v alues(1003)
        Go
        select * from table_1
        select * from table_2

        [/CODE]

        Update Query

        [CODE=sql]


        UPDATE Table_1 INNER JOIN Table_2
        ON Table_1.ID = Table_2.FK_ID
        SET Table_1.Code = Table_2.ID
        [/CODE]

        Please try it and help me to solve this.
        Thanks a Lot!

        Comment

        • vijaii
          New Member
          • May 2007
          • 15

          #5
          Originally posted by ajaxrand
          I create a sample table with this Queries.But failed when try to run the update.
          [CODE=sql]
          Create Table Table_1(
          ID int primary key Identity (1001,1),
          Code varchar(10) null
          )
          Go

          insert into Table_1(Code)va lues(null)
          insert into Table_1(Code)va lues(null)
          insert into Table_1(Code)va lues(null)

          Go
          Create Table Table_2(
          ID int primary key Identity (501,1),
          FK_ID int not null,
          FOREIGN KEY (FK_ID) REFERENCES Table_1(Id)
          )
          Go
          insert into table_2(FK_ID)v alues(1001)
          insert into table_2(FK_ID)v alues(1002)
          insert into table_2(FK_ID)v alues(1003)
          Go
          select * from table_1
          select * from table_2

          [/CODE]

          Update Query

          [CODE=sql]


          UPDATE Table_1 INNER JOIN Table_2
          ON Table_1.ID = Table_2.FK_ID
          SET Table_1.Code = Table_2.ID
          [/CODE]

          Please try it and help me to solve this.
          Thanks a Lot!

          Try this ...

          UPDATE TABLE_A
          SET A_CODE = B_ID
          FROM TABLE_A INNER JOIN TABLE_B
          ON A_ID = B_A_ID

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            Originally posted by vijaii
            Try this ...

            UPDATE TABLE_A
            SET A_CODE = B_ID
            FROM TABLE_A INNER JOIN TABLE_B
            ON A_ID = B_A_ID
            Does this one do the same thing as your one?
            (see my previous post for structure of tables)

            [CODE=sql]
            UPDATE table_1
            SET Code = table_2.ID
            FROM TABLE_2
            WHERE TABLE_1.ID = TABLE_2.FK_ID[/CODE]

            Or Any issues with this?

            Comment

            • vijaii
              New Member
              • May 2007
              • 15

              #7
              Originally posted by ajaxrand
              Does this one do the same thing as your one?
              (see my previous post for structure of tables)

              [CODE=sql]
              UPDATE table_1
              SET Code = table_2.ID
              FROM TABLE_2
              WHERE TABLE_1.ID = TABLE_2.FK_ID[/CODE]

              Or Any issues with this?
              You can do it in this way also.

              Comment

              • ak1dnar
                Recognized Expert Top Contributor
                • Jan 2007
                • 1584

                #8
                Originally posted by vijaii
                You can do it in this way also.
                Many Thanks! then no need to struggle with Joins.

                Comment

                Working...