April 29, 2013 at 3:47 am
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
April 29, 2013 at 4:06 am
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
April 29, 2013 at 4:09 am
any chance on how i would go about that?
April 29, 2013 at 4:26 am
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)
April 29, 2013 at 4:39 am
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.
April 29, 2013 at 4:45 am
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.
April 29, 2013 at 4:46 am
Who told you to do that? Please will you post the CREATE TABLE statement for Table A?
John
April 29, 2013 at 4:47 am
table b is just a lookup table and remains unchanged.
April 29, 2013 at 4:59 am
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.
April 30, 2013 at 2:40 am
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
May 1, 2013 at 8:17 am
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