Update Statement Help

  • Hi guys,

    Here is my sample Tables (TableA & TableB)

    Table1

    ID, ID_Link, WU, Level

    001,NULL , 110,1

    002,NULL ,120,2

    003,NULL, 125,3

    004,NULL,120,1

    Table2

    ID, NEXT_WUC,LVL

    1,120,2

    2,130,1

    3,120,1

    4,125,3

    The result should be like this,

    So end result should be

    ID, ID_Link, WU, Level

    001,NULL , 110,1

    002,002 ,120,2

    003,003, 125,3

    004,004,120,1

    I am using below Update statement, Please guide me where i am wrong.

    UPDATE A

    SET ID_Link = A.ID

    FROM Table1 A

    INNER JOIN Table2 AS B

    on B.Next_WUC = A.WU

    AND A.Level = B.LVL

    Thanks in Advance.

  • First of all let me say that you will most likely get a tested answer if you post your table definitions, sample data, and what you have attempted, following the format given when you click on the first link in my signature block.

    That said, and it being a very slow Saturday morning here is what I tested.

    CREATE TABLE #Table1(ID INT, ID_Link INT, WU INT, xLevel INT)

    INSERT INTO #Table1

    SELECT 001,NULL , 110,1 UNION ALL

    SELECT 002,NULL ,120,2 UNION ALL

    SELECT 003,NULL, 125,3 UNION ALL

    SELECT 004,NULL,120,1

    CREATE TABLE #Table2(ID INT, NEXT_WUC INT,LVL int)

    INSERT INTO #Table2

    SELECT 1,120,2 UNION ALL

    SELECT 2,130,1 UNION ALL

    SELECT 3,120,1 UNION ALL

    SELECT 4,125,3

    UPDATE A

    SET ID_Link = A.ID

    FROM #Table1 A

    INNER JOIN #Table2 AS B

    on B.Next_WUC = A.WU

    AND A.XLevel = B.LVL

    SELECT ID, ID_Link, WU, XLevel FROM #Table1

    My results:

    1NULL1101

    221202

    331253

    441201

    The above gave me the results that you say you wanted. So my question to you ... is their something you failed to post or is your T-SQL statement as posted different then the one you were/are actually utilizing ?

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply