declare @t table(id int,expr1 varchar(20),mail varchar(30))
insert @t 
select 1,'uakron.edu','[email protected]' union all
select 2,'uakron.edu','[email protected]' union all
select 3,'uakron.edu','[email protected]' union all
select 4,'uakron.edu','[email protected]' union all
select 5,'adrian.edu','[email protected]' union all
select 6,'adelphi.edu','[email protected]' union all
select 7,'adelphi.edu','[email protected]'
select rid = (select count(1)+ 1 from @t where expr1 = a.expr1 and id < a.id)
,* into #
from @t aselect * from #
declare @I int
set @I = 1
declare @j int
set @j = 1
declare @sql varchar(8000)
declare @count int
declare @counttemp int
set @counttemp = 0
select @count = count(*) from #
while  @count > @counttemp
begin
set @sql = 'select * into tempexpr'+convert(varchar,@j)+ ' from # where rid >= '+cast(@i as varchar)
+' and rid <' +cast(@I as varchar)+' + 3'
print @sql
exec(@sql)
select @counttemp = @counttemp + @@rowcount
select @I = @i + 3
set @j = @j +  1end
select * from tempexpr1
select * from tempexpr2
select * from tempexpr3 ---根据情况select不同表
drop table #,tempexpr1,tempexpr2,tempexpr3,tempexpr4,tempexpr5,tempexpr6
---根据情况drop不同表

解决方案 »

  1.   

    declare @t table(id int,expr1 varchar(20),mail varchar(30))
    insert @t 
    select 1,'uakron.edu','[email protected]' union all
    select 2,'uakron.edu','[email protected]' union all
    select 3,'uakron.edu','[email protected]' union all
    select 4,'uakron.edu','[email protected]' union all
    select 5,'adrian.edu','[email protected]' union all
    select 6,'adelphi.edu','[email protected]' union all
    select 7,'adelphi.edu','[email protected]'
    select rid = (select count(1)+ 1 from @t where expr1 = a.expr1 and id < a.id)
    ,* into #
    from @t aselect * from #
    declare @I int
    set @I = 1
    declare @j int
    set @j = 1
    declare @sql varchar(8000)
    declare @count int
    declare @counttemp int
    set @counttemp = 0
    select @count = count(*) from #
    while  @count > @counttemp
    begin
    set @sql = 'select * into tempexpr'+convert(varchar,@j)+ ' from # where rid >= '+cast(@i as varchar)
    +' and rid <' +cast(@I as varchar)+' + 3'
    print @sql
    exec(@sql)
    select @counttemp = @counttemp + @@rowcount
    select @I = @i + 3
    set @j = @j +  1end
    select * from tempexpr1
    select * from tempexpr2
    select * from tempexpr3 ---根据情况select不同表
    drop table #,tempexpr1,tempexpr2,tempexpr3,tempexpr4,tempexpr5,tempexpr6
    ---根据情况drop不同表
      

  2.   

    技术搞定了,下面是源码,也给后来的人看看吧.同时谢谢一楼的朋友!
    declare @i int,@n int,@count int ,@max int , @name_next varchar(50),@name_now varchar(50),@nameid int,@mail varchar(50)
    declare @tablename varchar(10),@sqlCreate varchar(1000),@sqlInsert varchar(1000)SELECT @max=MAX(max_expr1) FROM (SELECT COUNT(expr1) AS max_expr1 FROM aa GROUP BY expr1) DERIVEDTBL
    set @n=1
    update aa set flag=1
    while  @n<=(@max/3)+1
    begin
    set @count=0
    set @tablename = 'table' + convert(varchar(10),@n)
    set @sqlCreate = 'CREATE TABLE ' + @tablename + '(mail varchar(50) )'
    print(@sqlCreate)
    EXECUTE(@sqlCreate)
    set @i=1 
    while  @i<=15961
    begin
    select @nameid=flag from aa where id=@i
    if (@nameid!=0)
    begin
    select @name_now=expr1 from aa where id=@i

    select @mail=mail from aa where id=@i
    select @name_next =expr1 from aa where id =@i+1

    if (@count<3) --and @nameid=1@name_next=@name_now  and 
    begin
    --insert @tablename (mail) values (@mail) --where id=i

    set @sqlInsert='insert into ' + @tablename + ' (mail) ' + ' values  ('''+@mail+''' )'
    print(@sqlInsert)
    execute(@sqlInsert)
    update aa set flag=0 where id =@i
    if(@name_next=@name_now ) set @count=@count+1
    end
    else
    begin
    if(@name_next!=@name_now) set @count=0
    end
    end
    set @i=@i+1
    end 

    set @n=@n+1
    end