Update Join Query Help!!!!

  • hi Guys

    I need help with a simple update join query that i need to do. below are my example of the two tables:

    TABLE A

    DIVISION | SUB_UNIT | REASONSREFERRAL

    NULL NULL 4

    NULL NULL 2

    NULL NULL 3

    TABLE B (Lookup Table)

    ID | Description | ORDER

    2 workload issues 9

    3 work conditions 10

    4 work relationships 11

    I need to replace the numbers in the reasons for referral column in table a with the text in the descripton column in table b with a update join query. i know it sounds simple but i am relatively new to sql coding. any help with this. many thanks

  • You shouldn't do that, because you'll destroy your referential integrity, and chances are you can't do it anyway, since the REASONSREFERRAL column probably has a data type that won't allow the description in. What I recommend instead is that you create a view that joins the two tables and shows the DIVISION, SUB_UNIT and Description columns.

    John

  • any chance on how i would go about that?

  • Look up CREATE VIEW in Books Online, but to help:

    create view dbo.MyView

    as

    select

    a.DIVISION,

    a.SUB_UNIT,

    b.Description as ReasonsReferral

    from

    dbo.TableA a

    inner join dbo.TableB b

    on (a.REASONSREFERRAL = b.ID)

  • thanks but i am told not to use a view but to actually update the reasonforreferral column from table a with the description column from table B with an update join statement.

  • prb88 (4/29/2013)


    thanks but i am told not to use a view but to actually update the reasonforreferral column from table a with the description column from table B with an update join statement.

    Bad choice. That means everytime you change the description in TableB you will have to go back and update TableA with the new description, unless it is okay for the old description to remain.

  • Who told you to do that? Please will you post the CREATE TABLE statement for Table A?

    John

  • table b is just a lookup table and remains unchanged.

  • prb88 (4/29/2013)


    table b is just a lookup table and remains unchanged.

    In all my years in this business I have never seen a lookup table remain static. New values are added, old values deleted, and current values changed to meet business requirements and/or changes.

  • Hello prb88,

    why you should do that?. Your lookup table plays a role, it serves to maintain the description out of the main table, and your request just negates the meaning of the lookup table.

    In addition there is a chance for your update going wrong; you are updating a code (presumably an integer column) with a string value, so your update will never success.

    I will suppose your query is simply an example of what you want to do, not your concrete goal (it is too weird). In a generic manner, to update a table with the contents of another table yo should follow two steps:

    First step, code a SELECT that links the first table with the second table contents. As Lynn suggested,

    SELECT a.DIVISION, a.SUB_UNIT, a.ReasonsReferral, b.Description

    from dbo.TableA a

    inner join dbo.TableB b on a.REASONSREFERRAL = b.ID

    After coding that query simply substitute SELECT by UPDATE (check the syntax, I cannot check it now)

    UPDATE TableA SET a.ReasonsReferral = b.Description

    from dbo.TableA a

    inner join dbo.TableB b on a.REASONSREFERRAL = b.ID

    Regards,

    Francesc

  • In all my years in this business I have never seen a lookup table remain static. New values are added, old values deleted, and current values changed to meet business requirements and/or changes.

    +1

    In fact, I am making changes to a static lookup table right now.

Viewing 11 posts - 1 through 10 (of 10 total)

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