SQL Server,对于每条指令

发布于 2024-10-03 20:27:17 字数 362 浏览 2 评论 0原文

  1. 客户(Id,名称...)
  2. 产品(id,名称...)
  3. Clients_Products(client_id,product_id)

我添加了 10 个客户,我需要更新联接表(clients_products)并为每个产品和每个客户添加新记录。

有什么方法可以在 SQL Server Management Studio 中执行双循环或其他操作吗?

然后我删除了这 10 行,但我想创建这 10 行并将它们直接与所有产品连接。 (请改写最后一句话 - 毫无意义)

有什么帮助吗?谢谢,

我可以使用 vb .net 以编程方式完成此操作,但我认为有任何方法可以实现此目的。

  1. Clients (Id, Name...)
  2. Products (id, Name...)
  3. Clients_Products (client_id, product_id)

I added 10 Clients and i need to update the join table (clients_products) and add a new record for each product and each client.

Is there any way to do a double loop or something in SQL Server management studio or something?

Then I deleted the 10 rows, but i want to create these 10 rows and join them with all products directly. (please reword this last sentence - makes no sense)

any help? thx

i can do this programmatically with vb .net but i think that there would be any way to achieve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

同展鸳鸯锦 2024-10-10 20:27:17

这需要交叉连接

INSERT INTO clients_products (client_id, product_id)
SELECT c.ID, p.ID
FROM Clients c
CROSS JOIN Products p
LEFT JOIN clients_products cp
    on c.client_id and p.product_id
WHERE cp.client_id is null and p.product_id is null

This calls for a cross join

INSERT INTO clients_products (client_id, product_id)
SELECT c.ID, p.ID
FROM Clients c
CROSS JOIN Products p
LEFT JOIN clients_products cp
    on c.client_id and p.product_id
WHERE cp.client_id is null and p.product_id is null
懒的傷心 2024-10-10 20:27:17
INSERT INTO Clients_Products
SELECT c.Id, p.Id
FROM Clients c
JOIN Products p on 1=1
WHERE c.Name in ('NEW CLIENT 1', 'NEW CLIENT 2')
INSERT INTO Clients_Products
SELECT c.Id, p.Id
FROM Clients c
JOIN Products p on 1=1
WHERE c.Name in ('NEW CLIENT 1', 'NEW CLIENT 2')
故事灯 2024-10-10 20:27:17

我不确定我明白你在说什么,但听起来你想循环遍历 SQL 中的记录集。
请参阅:

http://msdn.microsoft.com/en-us/library/ ms180152.aspx

I'm not sure i understand what you are saying, but it sounds like you want to loop through a record set in SQL.
See this:

http://msdn.microsoft.com/en-us/library/ms180152.aspx

溺渁∝ 2024-10-10 20:27:17

如果我正确理解你的问题,那么你要求对两个表进行笛卡尔连接:

insert into clients_products  
select
  clients.id,products.id 
from clients
  cross join products

If i understood your question correctly, you are asking for a Cartesian join of both the table:

insert into clients_products  
select
  clients.id,products.id 
from clients
  cross join products
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文