简体   繁体   中英

Insert to a table all records from a column from another table with a standard value

I have two tables, shops and products. Table shop with columns shopId (PK) and shopName. Another table Products has columns productId (PK) and productName. Because I have a relationship many-many between two tables I create another table shopProduct with two columns shopId and productId.

For example I have shop1 with id=1 and I have shop2 with id=2, product1 with id=1 and product2 with id=2. and the records of the table shopProduct is 1-1 ,2-2.

But now I want to have a product3 with id=3 that apply to all shops.

I consider in the real problem that i have about 250 shops.

How can implement this without a stored procedure?

I don't want to do of course 250

insert into shopProduct (shopId, productId) 
values (@shopId,@productId).

Can I do for loop for example to feed the the shopId value every time?The other value for product id is the same every time.

From my understanding of this question, try this... Seems too simple, but...

Insert into ShipProduct (ProductID, ShopID)
Select 3, ShopID
From Shops

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM