简体   繁体   English

如何为SQL Server中另一个表中的所有行添加行

[英]How to add a row for all the rows in another table in SQL Server

I am not the best at the SQL language. 我不是SQL语言中最好的。 I have a table with approximately 20,000 users (rows) in it. 我有一个表,其中包含大约20,000个用户(行)。 I have another table, that I would like to add a row to for every user, using their username. 我有另一个表,我想使用他们的用户名为每个用户添加一行。 Is this possible using only SQL? 这只能使用SQL吗?

I could just go into the application (written in c#) and use linq to pull out all the users, iterate over them, and add a row for each user. 我可以进入应用程序(用c#编写)并使用linq拉出所有用户,迭代它们,并为每个用户添加一行。 I'm just curious if there is a way to do it in SQL directly. 我只是好奇是否有办法在SQL中直接执行它。

TABLE Users
Username (varchar)
etc
etc

TABLE ChatChannels
Username (varchar)
ChannelName (varchar)

I would like to add one row in ChatChannels for every user in Users, using the username to populate the Username column in ChatChannels. 我想在ChatChannels中为Users中的每个用户添加一行,使用用户名填充ChatChannels中的Username列。

insert into chatchannels (Username, ChannelName)
select username, 'NewChatChannel'
from users

This inserts one row per username in users , with the channelname set to 'NewChatChannel'. 这会在users为每个username插入一行,并将channelname设置为“NewChatChannel”。

暂无
暂无

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

相关问题 如何删除每行的 id 等于另一个表的 id 的所有行(SQL Server) - How to delete all the rows where each row's id is equal to another table's id (SQL Server) 另一个表的所有行上的行的SQL计算 - SQL Calculation of Row on All Rows of another table 如何在SQL Server表的所有行中添加唯一标识符 - How to add unique identifier in all rows of a SQL Server table 如何基于另一个SQL Server表的单列中的更改添加一个SQL Server表的行和时间戳 - How to add a row and timestamp one SQL Server table based on a change in a single column of another SQL Server table 如何在SQL Server 2012中将数据从另一个表的多个表行插入一个表行? - How to insert data into one table row from multiple table rows in another table in SQL Server 2012? 如何将一个行值移动到另一行并删除所有为空的SQL Server 2008 - How to move one row value to another row and remove all rows which are null SQL Server 2008 如何计算 SQL 表中每一行与另一个表的所有行的距离? - How do I calculate the distance of each row in an SQL table with all the rows of another table? sql(postgres)-如何创建一个触发器,将一个表中的两行相乘并在另一个表中将它们添加一行? - sql(postgres)- How to create a trigger that multiply two rows from a table and add them a row in another table? SQL Server:如果另一个表中的所有行均符合条件,则更新表 - SQL Server : update table if all rows in another table match a criteria SQL:查找表中的所有行,其中引用原始行的另一个表中的所有行都具有特定属性 - SQL: Find all rows in a table for which all rows in another table that refer to the original row have a certain property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM