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
 General SQL Server Forums
 New to SQL Server Programming
 Insert value only if other value exists

Author  Topic 

nikl
Starting Member

7 Posts

Posted - 2013-05-10 : 06:59:03
I am trying to add a value to all entries that already own another value.
I was hoping this code woudl do the trick:

SELECT value FROM catalog_product_entity_varchar
WHERE attribute_id=608 and value="XYZ"
UPDATE catalog_product_entity_varchar SET value = "XYZ_2"
WHERE attribute_id=668;

But instead of being only applied to the entries found via SELECT value FROM catalog_product_entity_varchar
WHERE attribute_id=608 and value="XYZ" it is applied to all entries. How can I make sure value XYZ_2 is only added to the entries that already include value XYZ?
Thank you!

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2013-05-10 : 07:07:54
UPDATE catalog_product_entity_varchar SET value = "XYZ_2"
WHERE attribute_id=668 and value='XYZ';

mohammad.javeed.ahmed@gmail.com
Go to Top of Page

nikl
Starting Member

7 Posts

Posted - 2013-05-10 : 07:31:46
This will replace XYZ with XYZ_2 in attribute668. However I need to add XYZ_2 to attribute668 if attribute608 has XYZ as value.

Isn't this possible or does my explanation make no sense?
Go to Top of Page

Mark W
Starting Member

2 Posts

Posted - 2013-05-10 : 10:27:32
quote:
Originally posted by nikl

This will replace XYZ with XYZ_2 in attribute668. However I need to add XYZ_2 to attribute668 if attribute608 has XYZ as value.

Isn't this possible or does my explanation make no sense?



If you trying to concatenate the two values, try this

UPDATE catalog_product_entity_varchar SET value = value + 'XYZ_2'
WHERE attribute_id=668 and value='XYZ';
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-05-10 : 10:51:38
It's much more helpfull to explain what you want to do in words. Then followup by showing sample data and expected output. Providing a query that doesn't work, doesn't realy help. That being said, here ar some links that can help you perpare your sample data in a consumable format. That will help describe teh problem and it gives us code we can run. In return you get a working solution.

http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

nikl
Starting Member

7 Posts

Posted - 2013-05-13 : 05:36:37
Ok I took a closer look at this and this is as good as I will be able to explain this with my limited to non-existent knowledge.
To prevent me from mixing up real db notations with shortened xyz-like ones, I will use my actual table notations.

I have a table 'catalog_product_entity_varchar' which includes these columns:
value_id, entity_type_id, attribute_id, store_id, entity_id, value
Entries in this table with 'attribute_id' = 608 can include the value "versandfrei_info" - but not necessarily.
I now need to find all entries with attribute_id = 608 and value = "versandfrei_info", take their entity_id and add to each entry in this 'catalog_product_entity_varchar' with matched entry_id and attribute_id = 668 the new value "versandfrei_info2".

Maybe this image will help to understand what I am trying to do:

As you can see there are two entries with matching entity_id, one with attribute_id 608, the other with 668. Now I need to check if attribute_id equals 608 and value equals "versandfrei_info" and then add "versandfrei_info2" for the entry that has the matching entity_id and whose attribute_id is 668.
Go to Top of Page
   

- Advertisement -