简体   繁体   English

SQL SERVER为现有表的每一行插入新列值

[英]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.

相关问题 SQL将新行插入到现有表中,更改除一列外的所有列 - SQL Insert a new row into an existing table changing all but one column 将具有来自 python 列表的值的新列插入到现有 sql 表中 - Insert new column with values from a python list into an existing sql table 如果 2 列值不存在,则在 SQL 表中插入新行数据 - Insert new row of data in SQL table if the 2 column values do not exist Sql Server触发器将值从新行插入另一个表 - Sql Server trigger insert values from new row into another table SQL触发器:一次将删除的行插入到表中,而无需将每一列都列为INSERT值 - SQL TRIGGER: Insert deleted row into a table at once without listing every column as INSERT values SQL 将值添加到现有表中的新列? - SQL adding values to a new column in an existing table? SQL - 根据另一个表条目将新行插入现有表 - SQL - Insert new row into an existing table based on another table entry SQL-尝试在现有表中插入新列,其中值从50开始自动递增1 - SQL - Trying to insert a new column in an existing table where the values are auto incremented by 1 starting from 50 SQL-从第二列插入行值(作为列数据插入新表) - SQL - insert row values from second column ( to new table as column data) sql-为表中的每个客户id插入新行 - sql - insert a new row for every customer id that is in the table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM