Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 update a table with info from another

Author  Topic 

ts_abbott@hotmail.com
Starting Member

36 Posts

Posted - 2004-11-04 : 13:21:32
Hi,

I need to update one of my tables with info from another but I'm unsure how to go about it.

These are the tables and sample data:

CarStock

Id | MakeId | RangeId | ModelId | CarCode | Colour | Price
1 | NULL | NULL | NULL | ABC12345 | basalt black | 65900
2 | NULL | NULL | NULL | ABV8uNHD | firedance orange | 14000


CarLookup

Id | CarCode | MakeId | RangeId | ModelId
1 | ABC12345 | 1 | 2 | 67
2 | ABV8uNHD | 5 | 6 | 39

* this table always has all 5 fields completed.


How would I go about writing an update statement to set the null values in CarStock to what they should be by joining CarStock.CarCode onto CarLookup.CarCode?

Any help would be much appreciated.

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-11-04 : 13:23:14
Why do you wish to store this info in 2 places??
Go to Top of Page

ts_abbott@hotmail.com
Starting Member

36 Posts

Posted - 2004-11-04 : 13:38:58
quote:
Originally posted by ehorn

Why do you wish to store this info in 2 places??



for performance reasons mainly. i've got a query which matches user's alerts against carstock via carlookup but it's far to slow. i've now modified carstock to include all fields that i need to join user's alerts directly onto carstock but the xml feed i receive that updates carstock only contains a carcode which is why i need to run a query to fill in the missing fields.

carlookup is essentially a table that contains every single car you can get down to the spec level (i.e. 1.8T gti 3dr).
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-11-04 : 14:04:26
something like this?


update t1
set MakeId = t2.MakeId,
RangeId = t2.RangeId,
ModelId = t2.ModelId
from CarStock t1
inner join CarLookup t2 on (t1.CarCode = t2.CarCode)


Go with the flow & have fun! Else fight the flow
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-11-04 : 14:11:48
With some filter criteria like this:


where cs.makeid is null or cs.rangeid is null or cs.modelid is null
Go to Top of Page

ts_abbott@hotmail.com
Starting Member

36 Posts

Posted - 2004-11-05 : 03:59:17
thanks spirit1 & ehorn that's exactly the code I was looking for! i've just tested it and it runs perfectly!
Go to Top of Page
   

- Advertisement -