日期:2014-05-19  浏览次数:20546 次

如何不重复的随即关联两张表的ID
在做一个考试考场的小东东,已经有两张表,一个考生表,一个是座位表,如何随即不重复的将两表的ID关联起来呢?而且数据量比较大。

------解决方案--------------------
其实就是获得2个对象的随机组合
那么创建2个临时表
create table #t1(ident1 int identity(1,1),StudentId)
create table #t2(ident2 int identity(1,1),SeatId)

--declare @count int
insert into #t1(StudentId) select StudentId from 学生表 order by newid()
--set @count=@@rowcount

--如果座位与学生数量差距不大 就直接
insert into #t2(SeatId) select SeatId from 座位表 order by newid()
/*
exec ( 'insert into #t2(SeatId) select top '+convert(varchar),@rowcount)+ ' SeatId from 座位表 order by newid() '
*/
select * from #t1,#t2 where ident1=ident2
就可以了