create table T1(GP1 int,GP2 varchar(10))
insert into T1 select 1,''
insert into T1 select 2,''
insert into T1 select 3,''
insert into T1 select 4,''
insert into T1 select 5,''
insert into T1 select 6,''
insert into T1 select 7,''
insert into T1 select 8,''create table T2(GP2 varchar(10))
insert into T2 select 'A'
insert into T2 select 'B'
insert into T2 select 'C'
select identity(int,0,1) id,GP1 into #T1 from T1
select identity(int,0,1) id,GP2 into #T2 from T2
declare @count int
select @count=count(*) from #T2
update a
set  
    GP2=c.GP2
from
    T1 a,
    #T1 b,
    #T2 c
where
    a.GP1=b.GP1 and c.id=b.id%@countselect * from T1
/*
GP1         GP2        
----------- ---------- 
1           A
2           B
3           C
4           A
5           B
6           C
7           A
8           B
*/drop table T1,T2,#T1,#T2

解决方案 »

  1.   

    alter table 表1 add GP2 char(1) nullupdate 表1 set GP2='A' where GP1%3=1
    update 表1 set GP2='B' where GP1%3=2
    update 表1 set GP2='C' where GP1%3=0
      

  2.   

    create table t_a(GP1 int,GP2 varchar(10))
    insert into t_a select 1,''
    insert into t_a select 2,''
    insert into t_a select 3,''
    insert into t_a select 4,''
    insert into t_a select 5,''
    insert into t_a select 6,''
    insert into t_a select 7,''
    insert into t_a select 8,''create table t_b(GP2 varchar(10))
    insert into t_b select 'A'
    insert into t_b select 'B'
    insert into t_b select 'C'
    insert into t_b select 'D'drop table #tdeclare @s varchar(8000),@i int,@j int
    select @s='',@j=1
    select id=identity(int,1,1),gp2,nid=0 into #t from t_b
    set @i=@@identity
    select @s=@s+' when gp1%' + cast(@i as varchar(10))+'='+cast(t.nid as varchar(10))+ ' then '''+t.gp2 + ''' '  from (select id,gp2,nid=id%@i from #t) t
    set @s='update t_a set gp2=case ' +@s + ' end '
    --print @s
    exec(@s)
    select * from t_a
      

  3.   

    declare @count int
    select identity(int,0,1) id,GP1 into #T1 from T1
    select identity(int,0,1) id,GP2 into #T2 from T2select @count=@@rowcount
    update a
    set  
        GP2=c.GP2
    from
        T1 a,
        #T1 b,
        #T2 c
    where
        a.GP1=b.GP1 and c.id=b.id%@count