Problem With SQL UPDATE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SAM

    Problem With SQL UPDATE

    HI,
    I want to know if is possible to update Table1.Col5 from an
    Table2.Col3 ??
    The Problem is Col1 and Col2 doesn't have a same size.
    Exemple of record.

    Table 1
    Col1 Col2 Col3 Col4 Col5
    10 4001 F1 1 NULL
    10 4001 F1 1 NULL
    10 4003 F5 3 NULL


    Table 2
    Col1 Col2 Col3
    10 4001 S
    10 4001 P
    10 5009 S
    24 4001 P

    I tried to update T1.Col5 with T2.Col3 by i got this result
    I used INNER join Table1 and Table2 on Col1 and Col2.

    Table 1
    Col1 Col2 Col3 Col4 Col5
    10 4001 F1 1 S
    10 4001 F1 1 S
    10 4003 F5 3 NULL

    Can you help me please.
    Thanks in advance.

  • David Portas

    #2
    Re: Problem With SQL UPDATE

    "SAM" <saberb@hotmail .comwrote in message
    news:5f09267d-5086-4b7e-b945-63dd587167f6@i2 9g2000prf.googl egroups.com...
    HI,
    I want to know if is possible to update Table1.Col5 from an
    Table2.Col3 ??
    The Problem is Col1 and Col2 doesn't have a same size.
    Exemple of record.
    >
    Table 1
    Col1 Col2 Col3 Col4 Col5
    10 4001 F1 1 NULL
    10 4001 F1 1 NULL
    10 4003 F5 3 NULL
    >
    >
    Table 2
    Col1 Col2 Col3
    10 4001 S
    10 4001 P
    10 5009 S
    24 4001 P
    >
    I tried to update T1.Col5 with T2.Col3 by i got this result
    I used INNER join Table1 and Table2 on Col1 and Col2.
    >
    Table 1
    Col1 Col2 Col3 Col4 Col5
    10 4001 F1 1 S
    10 4001 F1 1 S
    10 4003 F5 3 NULL
    >
    Can you help me please.
    Thanks in advance.
    >
    I don't understand what end result you want. Please could you post your
    required results together with some explanation. I'd guess:

    UPDATE table1
    SET col5 =
    (SELECT col3
    FROM table2
    WHERE table2.col1 = table1.col1
    AND table2.col2 = table2.col2);

    --
    David Portas


    Comment

    • SAM

      #3
      Re: Problem With SQL UPDATE

      On Feb 15, 3:30 pm, "David Portas"
      <REMOVE_BEFORE_ REPLYING_dpor.. .@acm.orgwrote:
      "SAM" <sab...@hotmail .comwrote in message
      >
      news:5f09267d-5086-4b7e-b945-63dd587167f6@i2 9g2000prf.googl egroups.com...
      >
      >
      >
      >
      >
      HI,
      I want to know if is possible to update  Table1.Col5 from an
      Table2.Col3 ??
      The Problem is Col1 and Col2 doesn't have a same size.
      Exemple of record.
      >
      Table 1
      Col1 Col2  Col3 Col4 Col5
      10    4001  F1    1    NULL
      10    4001  F1    1    NULL
      10    4003  F5    3    NULL
      >
      Table 2
      Col1 Col2 Col3
      10   4001   S
      10   4001   P
      10   5009   S
      24   4001   P
      >
      I tried to update T1.Col5 with T2.Col3 by i got this result
      I used INNER join Table1 and Table2 on Col1 and Col2.
      >
      Table 1
      Col1 Col2  Col3 Col4 Col5
      10    4001  F1    1    S
      10    4001  F1    1    S
      10    4003  F5    3    NULL
      >
      Can you help me please.
      Thanks in advance.
      >
      I don't understand what end result you want. Please could you post your
      required results together with some explanation. I'd guess:
      >
      UPDATE table1
      SET col5 =
       (SELECT col3
        FROM table2
        WHERE table2.col1 = table1.col1
         AND table2.col2 = table2.col2);
      >
      --
      David Portas- Hide quoted text -
      >
      - Show quoted text -
      HI,
      I got this result
      Table 1
      Col1 Col2 Col3 Col4 Col5
      10 4001 F1 1 S
      10 4001 F1 1 S
      10 4003 F5 3 NULL
      but
      my required results is

      Table 1
      Col1 Col2 Col3 Col4 Col5
      10 4001 F1 1 S
      10 4001 F1 1 P<--------
      10 4003 F5 3 NULL
      i want a 'P' en the second record not a 'S'


      thanks for your help

      Comment

      • David Portas

        #4
        Re: Problem With SQL UPDATE

        "SAM" <saberb@hotmail .comwrote in message
        news:4ac7cb13-9b8a-46a3-be44-96c2ea23b8d0@i2 9g2000prf.googl egroups.com...
        i want a 'P' en the second record not a 'S'
        Please explain how you arrive at that result. Also, what are the keys of the
        two tables? Every table should have a key but Table1 has none that I can
        see. As Erland says, the easiest way to describe your problem is to post a
        CREATE TABLE (with keys please) and some INSERTs.

        --
        David Portas


        Comment

        Working...