开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜