Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. newbie question: update across 2 tables

newbie question: update across 2 tables

Scheduled Pinned Locked Moved Database
questiondatabasetutorialannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spin vector
    wrote on last edited by
    #1

    I have 2 tables. I want to use info from the second table to update column values in the first. Consider: T1: id time1 time2 1 8:00 null 2 8:01 null 3 8:02 null 4 8:12 null T2: id time2 2 8:09 4 8:15 What I want is: T1: id time1 time2 1 8:00 null 2 8:01 8:09 3 8:02 null 4 8:12 8:15 Is there an sql command sequence that can achieve this? Its easy to think about it iteratively, but in the absense of explicit iterators in sql (to my understanding) I hope that there's another way. Its clear that I can exec the join: select T1.id, T1.time2 from T1, T2 where T1.id = T2.id but using this as part of an update is broken (well, because I don't know how to do it): (wrong:) update T1 set time2 = ( select time2 from T1, T2 where T1.id = T2.id) where id = ( select id from T1, T2 where T1.id = T2.id ) how can I write an on T1.time2 that matches T1.id = T2.id and updates the T1.time2 null with the T2.time2 ?

    M 1 Reply Last reply
    0
    • S spin vector

      I have 2 tables. I want to use info from the second table to update column values in the first. Consider: T1: id time1 time2 1 8:00 null 2 8:01 null 3 8:02 null 4 8:12 null T2: id time2 2 8:09 4 8:15 What I want is: T1: id time1 time2 1 8:00 null 2 8:01 8:09 3 8:02 null 4 8:12 8:15 Is there an sql command sequence that can achieve this? Its easy to think about it iteratively, but in the absense of explicit iterators in sql (to my understanding) I hope that there's another way. Its clear that I can exec the join: select T1.id, T1.time2 from T1, T2 where T1.id = T2.id but using this as part of an update is broken (well, because I don't know how to do it): (wrong:) update T1 set time2 = ( select time2 from T1, T2 where T1.id = T2.id) where id = ( select id from T1, T2 where T1.id = T2.id ) how can I write an on T1.time2 that matches T1.id = T2.id and updates the T1.time2 null with the T2.time2 ?

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      UPDATE T1
      SET time2 = T2.time2
      FROM T2
      WHERE T1.id = T2.id

      Tested with SQL Server 2000 SP4. You basically have an implicit cross-join with the table you're updating and the tables and join statements listed in the FROM clause.

      Stability. What an interesting concept. -- Chris Maunder

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups