sql - Update x columns from many rows in a different table -
i have table of products looks this:
products (companyid int, product1 int, product2 int ... product8 int)
and table of product details so:
productdetails (companyid int productid int)
my productdetails table has (at most) 8 rows of products each companyid.
i update products table products in each row (8 of them) values of products productdetails table rows products.companyid = productdetails.companyid
is there way without loop (i.e 1 update command)?
edit:
so idea products table contains company information (no product info) , productdetails table contains info 1-8 products / company.
so 1 update command should update products table each row (product1, product2 ... product8) products found in productdetails table each company.
you want pivot data table, allowing 8 columns. example below populates 8 products, if there more, simple don't inserted anywhere.
create table company ( companyid int, name varchar(100)); insert company values (1203, 'apple'); insert company values (7707, 'microsoft'); create table products ( companyid int, product1 varchar(100), product2 varchar(100), product3 varchar(100), product4 varchar(100), product5 varchar(100), product6 varchar(100), product7 varchar(100), product8 varchar(100)); create table productdetails ( companyid int, product varchar(100)); insert productdetails values (1203, 'ipad'); insert productdetails values (1203, 'iphone'); insert productdetails values (1203, 'ipod'); insert productdetails values (7707, 'visual studio'); insert productdetails values (7707, 'office'); insert products select * ( select companyid, product, rn=row_number() on (partition companyid order product) productdetails ) source pivot (max(product) rn in ([1],[2],[3],[4],[5],[6],[7],[8])) pv;
Comments
Post a Comment