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.
| Author |
Topic |
|
truthseeker
Starting Member
15 Posts |
Posted - 2010-09-30 : 13:12:20
|
| Hi gurus!I hope you can help me with this. I have two tables: Products and NewProducts. Products has 3 columns: sku, brand, activeNewProducts has 2 columns: sku, brandThe Products table has many brands, the NewProducts table has only 1 particular brand.What I need to do is if a certain product in the Products table does not appear in the NewProducts table, then Products.Active would be set to 0 with the following conditions:1. The Products.SKU does not appear in the NewProducts table.2. Only those Products with the same brand in the NewProducts table will be affected.I tried the following query but all the records are being affected.UPDATE ProductsSET active = 0FROM Product, NewProductWHERE Product.SKU NOT IN(SELECT NewProduct.SKU FROM Product, NewProductWHERE NewProduct.Brand = Product.Brand)Thanks for your inputs! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-09-30 : 13:15:51
|
| [code]UPDATE ProductsSET active = 0WHERE NOT EXISTS(SELECT 1 FROM NewProductWHERE NewProduct.Brand = Product.BrandAND NewProduct.Sku= Product.Sku)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
truthseeker
Starting Member
15 Posts |
Posted - 2010-09-30 : 13:48:04
|
quote: Originally posted by visakh16
UPDATE ProductsSET active = 0WHERE NOT EXISTS(SELECT 1 FROM NewProductWHERE NewProduct.Brand = Product.BrandAND NewProduct.Sku= Product.Sku) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Thanks, Visakh16. I tried this but with the same result. Let's say Products table have these rows:SKU Brand Activea x 1b y 1c z 1d y 1NewProducts has this:SKU Brande yWhen I run my and your query the results returnSKU Brand Activea x 0b y 0c z 0d y 0Which should not be. The Active field must still be 1 because though the 2nd and 4th row in the Products table do have the same Brand as that one in the NewProduct table; nevertheless, they have no matching SKUs. The SKU and the Brand in the Products table MUST match with the SKU and Brand in the other table.Now let's say the NewProduct table has this value:SKU Brandb yThen the result of the update should be:SKU Brand Activea x 0b y 1c z 0d y 0I hope I'm stating it clearly |
 |
|
|
tosscrosby
Aged Yak Warrior
676 Posts |
Posted - 2010-09-30 : 15:11:17
|
| You are contradicting yourself. If this comment states it clearly "The SKU and the Brand in the Products table MUST match with the SKU and Brand in the other table.", then Visakh's code works as is with your original data set. The brand (y) matches but there isn't a correpsonding SKU (e) so it sets the records to inactive. The second dataset would work with his code as well, provided the active flag was already set to "1" on the product table.If this comment states it clearly, "The Active field must still be 1 because though the 2nd and 4th row in the Products table do have the same Brand as that one in the NewProduct table; nevertheless, they have no matching SKUs", simply remove 'AND NewProduct.Sku= Product.Sku' from Visakh's code.Terry-- You can't be late until you show up. |
 |
|
|
|
|
|
|
|