0

I copinig here date from TABLE_PRODUCT that have info in 30 rows to TABLE_PRODUCT_CATEGORY_ORDER with 3 rows this script is working i i do this one by one there are any way to make this script shorter?

         INSERT INTO TABLE_PRODUCT_CATEGORY_ORDER (ProductShortID,CategoryID,Priority) SELECT TABLE_PRODUCT.ID,TABLE_PRODUCT.ProductCat ,TABLE_PRODUCT.ProductPriority    FROM TABLE_PRODUCT 
            INSERT INTO TABLE_PRODUCT_CATEGORY_ORDER (ProductShortID,CategoryID,Priority) SELECT TABLE_PRODUCT.ID,TABLE_PRODUCT.ProductSubCat1 ,TABLE_PRODUCT.ProductPriority1    FROM TABLE_PRODUCT 
            INSERT INTO TABLE_PRODUCT_CATEGORY_ORDER (ProductShortID,CategoryID,Priority) SELECT TABLE_PRODUCT.ID,TABLE_PRODUCT.ProductSubCat2 ,TABLE_PRODUCT.ProductPriority2    FROM ............
............
..........


            INSERT INTO TABLE_PRODUCT_CATEGORY_ORDER (ProductShortID,CategoryID,Priority) SELECT TABLE_PRODUCT.ID,TABLE_PRODUCT.ProductSubCat9 ,TABLE_PRODUCT.ProductPriority9    FROM TABLE_PRODUCT
2
  • You actually mean "copy data from 30 columns to [a] new table with 3 columns", don't you? Jun 27, 2013 at 12:01
  • Important this is Access 2010 and yes and this 30 comes with pairs like id1 name1,id2 name2,,,,,id9 name9 all this to new table with row id name that will contains all id1 id2....and name1,,,name2... Jun 27, 2013 at 12:02

1 Answer 1

1

You can combine all the values using UNION before inserting them.

INSERT INTO TABLE_PRODUCT_CATEGORY_ORDER (ProductShortID, CategoryID, Priority) 
SELECT *
FROM (SELECT ID, ProductCat AS CategoryID, ProductPriority AS Priority FROM TABLE_PRODUCT UNION 
SELECT ID, ProductSubCat1, ProductPriority1 FROM TABLE_PRODUCT UNION 
SELECT ID, ProductSubCat9, ProductPriority9 FROM TABLE_PRODUCT) 

UNION ALL would also include any duplicated rows (UNION drops these, if any).

2
  • It says i have unknown field name ProductCat but i check it twice this is the right name Jun 27, 2013 at 12:20
  • I've revised my answer to include aliases (AS) and I have been able to run this in Access 2010.
    – Andy G
    Jun 27, 2013 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.