How do UPDATE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Scatman
    New Member
    • Oct 2006
    • 8

    How do UPDATE

    I have got tree tables.
    t1 - ID, STATUS
    t2 - ID, ID_P
    t3 - ID_P, ID_OBJECT

    I need UPDATE table t1 and set up STATUS on OK in case that for the ID in t1 exists ID_OBJECT = ID0041 in table t3. Can someone give me advise please?
  • scripto
    New Member
    • Oct 2006
    • 143

    #2
    Originally posted by Scatman
    I have got tree tables.
    t1 - ID, STATUS
    t2 - ID, ID_P
    t3 - ID_P, ID_OBJECT

    I need UPDATE table t1 and set up STATUS on OK in case that for the ID in t1 exists ID_OBJECT = ID0041 in table t3. Can someone give me advise please?
    this should help

    if exists(select ID_P from t3 where ID_OBJECT = ID0041)
    begin
    update ti set STATUS = 'OK' where ID = (select ID_P from t3 where ID_OBJECT = ID0041)
    end

    Comment

    • Taftheman
      New Member
      • Nov 2006
      • 93

      #3
      select Count(ID_P) As Check from t3 where ID_OBJECT = ID0041

      If check >0
      begin
      update ti set STATUS = 'OK' where ID = (select ID_P from t3 where ID_OBJECT = ID0041)
      end

      Comment

      • almaz
        Recognized Expert New Member
        • Dec 2006
        • 168

        #4
        Originally posted by Taftheman
        select Count(ID_P) As Check from t3 where ID_OBJECT = ID0041

        If check >0
        begin
        update ti set STATUS = 'OK' where ID = (select ID_P from t3 where ID_OBJECT = ID0041)
        end
        I assume that there is a many-to-many relationship between t1 and t3. If yes, than the query should look:
        Code:
        update t1 
        set STATUS = 'OK' 
        where ID in 
        (  select ID from t2 where ID_P in 
             (select ID_P from t3 where ID_OBJECT = ID0041)
        )

        Comment

        Working...