declare @i int
select @i=max(classNumber) from 表a
set rowcount @i
select id=identity(int) into # from syscolumns a,syscolumns b
set rowcount 0select a.classID,b.TimeID
from 表a a,表b b,# c
where a.classNumber>=c.iddrop table #

解决方案 »

  1.   

    create table a(classID varchar(10),className nvarchar(20),classNumber int)
    create table b(TimeID varchar(10),TimeDes nvarchar(20))
    insert into a select '20011231','计算机一班',30
    union  all  select '20011232','计算机二班',25
    insert into b select '第一场','9:00-10:00'
    union  all  select '第二场','11:00-12:00'
    go
    alter table a add id int identity(1,1)
    go
    alter table b add id int identity(1,1)
    godeclare @classid varchar(10),@timeid varchar(10),@classNumber int
    declare test cursor for select a.classid,b.timeid,classnumber from a inner join b on a.id=b.id
    create table result(classID varchar(10),TimeID varchar(10))
    open test
    fetch next from test
    into @classid,@timeid,@classNumber
    while @@fetch_status=0
    begin
      declare @i int
      set @i=1
      while @i<=@classNumber
        begin
          insert into result select @classid,@timeid
          set @i=@i+1
        end
      fetch next from test
      into @classid,@timeid,@classNumber
    end
    select * from resultclose test
    deallocate testdrop table result
    drop table a,b
      

  2.   

    用臨時表里放n條PKID值,和原表的PKID進行inner join