Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Replace

Status
Not open for further replies.

jshurst

Programmer
Joined
Oct 27, 2004
Messages
1,158
Location
US
I have two tables. tblProduct and tblProductCrossReference. Let's say that tblProduct only has one column (ProductID), and the second table has two columns (OldProductID and NewProductID).

I want to compare all values in the first table to the second one and if I find that tblProduct.ProductID and tblProductCrossReference.OldProductID are equal I would like to replace the value of tblProduct.ProductID with tblProductCrossReference.NewProductID.

Can someone help me out with this? Thanks in advance.
 
Maybe,
Code:
UPDATE tblProduct
 SET tblProduct.ProductID = tblProductCrossReference.NewProductID
 FROM tblProduct
  LEFT OUTER JOIN tblProductCrossReference
    ON tblProduct.ProductID = tblProductCrossReference.OldProductID

As always, test it first.

-SQLBill

Posting advice: FAQ481-4875
 
Almost. The only problem is that I want it to skip the ones that are not found. This query sets the values to <NULL> if a value is not found in the ProductCrossReference table. I want it to just leave these values alone.

Thanks for the help so far.
 
It seems to work if I use RIGHT OUTER JOIN. Is this correct?
 
I used just JOIN too, but I can't tell a difference in the results.
 
Maybe:
Code:
UPDATE tblProduct
 SET tblProduct.ProductID = tblProductCrossReference.NewProductID
 FROM tblProduct
  LEFT OUTER JOIN tblProductCrossReference
    ON tblProduct.ProductID = tblProductCrossReference.OldProductID
WHERE tblProductCrossReference.NewProductID IS NOT NULL

-SQLBill

Posting advice: FAQ481-4875
 
I have two tables. tblProduct and tblProductCrossReference. Let's say that tblProduct only has one column (ProductID), and the second table has two columns (OldProductID and NewProductID).

I want to compare all values in the first table to the second one and if I find that tblProduct.ProductID and tblProductCrossReference.OldProductID are equal I would like to replace the value of tblProduct.ProductID with tblProductCrossReference.NewProductID.

do this:

update tblProduct, tblProductCrossReference
set tblProduct.productID = tblProductCrossReference.NewProductID
where tblProduct.ProductID =
tblCrossReference.OldProductID

it may seem that you're updating two tables, but, in actuality, you're setting only one column.
i have done this many times, where i update missing county names from a table that contains county names and county numbers.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top