开发者

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

开发者 https://www.devze.com 2023-02-09 21:37 出处:网络
I am not the best at the SQL language. I have a table with approximately 20,000 users (rows) in it. I have another table, that I would like to add a row to for every user, using their username. Is thi

I am not the best at the SQL language. I have a table with approximately 20,000 users (rows) in it. 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?

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. I'm just curious if there is a way to do it in SQL directly.

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.


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

This inserts one row per username in users, with the channelname set to 'NewChatChannel'.

0

精彩评论

暂无评论...
验证码 换一张
取 消