Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Update Join Query Help!!!! Expand / Collapse
Author
Message
Posted Monday, April 29, 2013 3:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 17, 2013 6:23 AM
Points: 15, 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

Post #1447426
Posted Monday, April 29, 2013 4:06 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:24 AM
Points: 5,430, Visits: 10,099
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
Post #1447432
Posted Monday, April 29, 2013 4:09 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 17, 2013 6:23 AM
Points: 15, Visits: 50
any chance on how i would go about that?
Post #1447434
Posted Monday, April 29, 2013 4:26 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:24 PM
Points: 20,739, Visits: 32,526
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)





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)
Post #1447439
Posted Monday, April 29, 2013 4:39 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 17, 2013 6:23 AM
Points: 15, 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.
Post #1447441
Posted Monday, April 29, 2013 4:45 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:24 PM
Points: 20,739, Visits: 32,526
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.



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)
Post #1447442
Posted Monday, April 29, 2013 4:46 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:24 AM
Points: 5,430, Visits: 10,099
Who told you to do that? Please will you post the CREATE TABLE statement for Table A?

John
Post #1447443
Posted Monday, April 29, 2013 4:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 17, 2013 6:23 AM
Points: 15, Visits: 50
table b is just a lookup table and remains unchanged.
Post #1447444
Posted Monday, April 29, 2013 4:59 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:24 PM
Points: 20,739, Visits: 32,526
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.



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)
Post #1447449
Posted Tuesday, April 30, 2013 2:40 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, May 8, 2014 6:52 AM
Points: 634, Visits: 809
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
Post #1447922
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse