简体   繁体   English

将具有标准值的另一个表的列中的所有记录插入到表中

[英]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. 带有shopId(PK)和shopName列的表shop。 Another table Products has columns productId (PK) and productName. 另一个表Products的列productId(PK)和productName。 Because I have a relationship many-many between two tables I create another table shopProduct with two columns shopId and productId. 因为我在两个表之间有很多关系,所以我创建了另一个具有两个列shopId和productId的表shopProduct。

For example I have shop1 with id=1 and I have shop2 with id=2, product1 with id=1 and product2 with id=2. 例如,我有id为1的shop1和id为2的shop2,id = 1的product1和id = 2的product2。 and the records of the table shopProduct is 1-1 ,2-2. 并且表shopProduct的记录是1-1,2-2。

But now I want to have a product3 with id=3 that apply to all shops. 但是现在我想拥有一个id3 = 3的product3,它适用于所有商店。

I consider in the real problem that i have about 250 shops. 我考虑的真正问题是我大约有250家商店。

How can implement this without a stored procedure? 没有存储过程如何实现呢?

I don't want to do of course 250 我当然不想做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. 我是否可以进行for循环来例如每次都输入shopId值?产品id的其他值每次都相同。

From my understanding of this question, try this... Seems too simple, but... 以我对这个问题的理解,尝试一下...似乎太简单了,但是...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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