SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update Join Query Help!!!!


Update Join Query Help!!!!

Author
Message
prb88
prb88
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 50
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
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14462 Visits: 15980
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
prb88
prb88
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 50
any chance on how i would go about that?
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40242 Visits: 38567
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)




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
prb88
prb88
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 50
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.
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40242 Visits: 38567
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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14462 Visits: 15980
Who told you to do that? Please will you post the CREATE TABLE statement for Table A?

John
prb88
prb88
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 50
table b is just a lookup table and remains unchanged.
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40242 Visits: 38567
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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
gofrancesc
gofrancesc
Right there with Babe
Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)

Group: General Forum Members
Points: 767 Visits: 812
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search