首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

|M| 求两表进行插入多条记录SQL解决方法

2012-02-24 
|M| 求两表进行插入多条记录SQL如有表UserID User1A2B3C4D5E表模块ModuleID UserID112131----------------

|M| 求两表进行插入多条记录SQL
如有表User
ID User
1 A
2 B
3 C
4 D
5 E

表模块
ModuleID UserID
1 1
2 1
3 1
---------------------------
然后现在要将User ID >3 的用户插到模块表中和用户1模块相同 最后为
ModuleID UserID
1 1
2 1
3 1
1 4
2 4
3 4
1 5
2 5
3 5

Sql语句要怎么写 谢谢

[解决办法]
顶一下哈
[解决办法]
create table T1(ID int, [User] varchar(100))


insert into T1 select 1, 'A'
insert into T1 select 2, 'B' 
insert into T1 select 3, 'C'
insert into T1 select 4, 'D' 
insert into T1 select 5, 'E' 

create table T2(ModuleID int, UserID int)


insert into T2 select 1, 1 
insert into T2 select 2, 1 
insert into T2 select 3, 1 


insert into T2
select b.ModuleID,a.id
from (select * from T1 where ID>3) as a inner join (select * from T2 where UserID=1) as b on 1=1

select * from T2

drop table T1,T2


/*
--结果
ModuleID UserID
1 1
2 1
3 1
1 4
2 4
3 4
1 5
2 5
3 5



*/

热点排行