[英]SQL SERVER Insert New Column Values for every row of existing table
I have an existing table of customers. 我有一个现有的客户表。 I want to add a NEW row for each customer based on ALL of the services our company offers.
我想根据我们公司提供的所有服务为每个客户添加一个新行。
AS IS: 原样:
Customer - ID
Freddy - 123
TO BE: 成为:
Customer - ID - Service
Freddy - 123 - Serv1
Freddy - 123 - Serv2
Freddy - 123 - Serv3
etc ... 等...
Right now I have been working with below but it has not given desired output: 现在我一直在与下面的工作,但它没有给出期望的输出:
INSERT INTO Customers (Service)
VALUES ('Serv1'), ('Serv2'), ('Serv3')
You need some sort of JOIN
, in this case a CROSS JOIN
: 您需要某种
JOIN
,在这种情况下为CROSS JOIN
:
SELECT c.Customer, c.Id, v.s
FROM Customers c CROSS JOIN
(VALUES ('Serv1'), ('Serv2'), ('Serv3')) v(s);
However, it doesn't make sense to insert this into the original table. 但是,将其插入原始表中没有任何意义。 That table only has two columns, but you seem to want a third one.
该表只有两列,但是您似乎想要第三列。
you have only two column so create third one. 您只有两列,因此请创建第三列。
go to table>>design>>and create the third row called Services 转到表>>设计>>,然后创建名为“服务”的第三行
now do this: 现在执行此操作:
UPDATE Customers
SET service='serv1'
WHERE Customer='Freddy '
Remember: The WHERE clause specifies which record or records that should be updated. 请记住:WHERE子句指定应更新的记录。 If you omit the WHERE clause, all records will be updated!
如果省略WHERE子句,所有记录将被更新!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.