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