update b 
set Account = case when len(a.Account)<6 
                    then a.ID+(REPLICATE('0', 6-len(a.Account))+a.Account))
                    else a.Account End 
from a,b,c
where a.OldAccout = b.Account and b.Uid=c.Uid and c.Code = '1' and b.flag = 'A'

解决方案 »

  1.   

    楼主怎么一个问题做两个问啊。是不是想多送点分给我啊,呵呵上面的括号多了半个,干脆去掉好了update b 
    set Account = case when len(a.Account)<6 
                        then a.ID+REPLICATE('0', 6-len(a.Account))+a.Account
                        else a.Account End 
    from a,b,c
    where a.OldAccout = b.Account and b.Uid=c.Uid and c.Code = '1' and b.flag = 'A'
      

  2.   

    --看看这个可以吗?update B表 set Account=cast(A.ID as varchar(4))+replicate('0',6-len(Account))+Account
    from A表 a join B表 b on a.Account=b.Account
               join C表 c on b.Uid=c.Uid
    where B.Flag = 'A'和C.Code='1'
      

  3.   

    --上边的有点错误.看看这个!update B表 set Account=cast(A.ID as varchar(4))+replicate('0',6-len(Account))+Account
    from A表 a join B表 b on a.Account=b.Account
               join C表 c on b.Uid=c.Uid
    where B.Flag = 'A' and C.Code='1' and len(a.Account)<6
      

  4.   

    update b 
    set b.Account = case when len(b.Account)<6 
                        then cast(a.ID as char)+REPLICATE('0', 6-len(b.Account))+a.Account)
                        else cast(a.ID as char)+b.Account End 
    from a,b,c
    where a.Account = b.Account and b.Uid=c.Uid and c.Code = '1' and b.flag = 'A'
      

  5.   

    update B 
        set Account = a.ID+right('000000'+B.Account,6)
    from 
        A , B , C
    where 
        A.Account = B.Account 
    and 
        B.Uid = C.Uid 
    and 
        C.Code = '1' 
    and 
        B.flag = 'A' 
    and 
        len(B.Account) < 6
      

  6.   

    提醒下:要注意a.ID的数据类型转换然后再进行字符串相加
      

  7.   

    Id是int型,Account是char型,怎样加到Account的前面?
    update b 
    set Account = case when len(a.Account)<6 
                        then a.ID+REPLICATE('0', 6-len(a.Account))+a.Account
                        else a.Account End 
    from a,b,c
    where a.OldAccout = b.Account and b.Uid=c.Uid and c.Code = '1' and b.flag = 'A'
    这样加会不会有问题?
      

  8.   

    不好意思,我上上面的有点错:
    改成这个:update b 
    set b.Account = case when len(b.Account)<6 
                        then cast(a.ID as char)+REPLICATE('0', 6-len(b.Account))+a.Account
                        else cast(a.ID as char)+b.Account End 
    from a,b,c
    where a.Account = b.Account and b.Uid=c.Uid and c.Code = '1' and b.flag = 'A'
      

  9.   

    Id是int   4
    a.Id as char不能把int转成char,会报错.
      

  10.   

    你的a.ID的最大值是多少?
    declare @i int
    set @i=1000000000
    select @i
    select cast(@i as char)这个转换没有问题
      

  11.   

    你把B表的Account长度定义为char(10)引起长度不够而截断了,你修改下该表结构alter table B表 alter column Account char(20) not null
      

  12.   

    --看错了,是char字段类型的影响, 这样写就行了:update B set Account=rtrim(a.Id)+right('000000'+rtrim(b.Account),6)
    from A,B,C
    where a.Account=b.Account and b.Uid=c.Uid  --关联条件
    and B.Flag = 'A' and C.Code='1'  --更新条件
    and len(b.Account)<6  --不足6位的才更新
      

  13.   

    --或者这样:update B set Account=cast(a.ID as varchar)+REPLICATE('0', 6-len(b.Account))+a.Account
    from A,B,C
    where a.Account=b.Account and b.Uid=c.Uid  --关联条件
    and B.Flag = 'A' and C.Code='1'  --更新条件
    and len(b.Account)<6  --不足6位的才更新
      

  14.   

    --测试--测试数据
    create table A(Account char(10),Id int)
    insert  A select '25001',11
    union all select '002'  ,12
    union all select '3003' ,13create table B(Uid int,Account char(10),name varchar(10),Flag char(1))
    insert  B select 1,'25001','a','A'
    union all select 2,'002'  ,'b','A'
    union all select 3,'3003' ,'c','B'create table C(Uid int,Code char(10))
    insert  C select 1,2
    union all select 2,1
    union all select 3,2
    go--更新
    update B set Account=rtrim(a.Id)+right('000000'+rtrim(b.Account),6)
    from A,B,C
    where a.Account=b.Account and b.Uid=c.Uid  --关联条件
    and B.Flag = 'A' and C.Code='1'  --更新条件
    and len(b.Account)<6  --不足6位的才更新--显示更新结果
    select * from B
    go--删除测试
    drop table A,B,C/*--测试结果Uid         Account    name       Flag 
    ----------- ---------- ---------- ---- 
    1           25001      a          A
    2           12000002   b          A
    3           3003       c          B(所影响的行数为 3 行)
    --*/
      

  15.   

    这样不出错了,不过当Account刚好是6位的时候不能实现不补0直接在前面加上id.
    有没有更好的方法,没有的话再执行下面这条会不会有什么问题?
    update B set Account=rtrim(a.Id) + rtrim(b.Account)
    from A,B,C
    where a.Account=b.Account and b.Uid=c.Uid  --关联条件
    and B.Flag = 'A' and C.Code='1'   --更新条件
    and len(b.Account)=6              --刚好6位的才更新
      

  16.   

    呵呵,忘记char是固定长度的啦,改成这样吧:update b 
    set b.Account = case when len(b.Account)<6 
                        then cast(a.ID as varchar)+REPLICATE('0', 6-len(rtrim(b.Account)))+rtrim(a.Account)
                        else cast(a.ID as varchar)+rtrim(b.Account)
    End 
    from a,b,c
    where a.Account = b.Account and b.Uid=c.Uid and c.Code = '1' and b.flag = 'A'
      

  17.   

    --因为楼主前面有一句"B表的Account不足六位先在前面补0然后把A表的Id加在0前面"
    --所以以为只处理不足6位的,如果是够6位的也要处理,则改成:
    update B set Account=rtrim(a.Id)+right('000000'+rtrim(b.Account),6)
    from A,B,C
    where a.Account=b.Account and b.Uid=c.Uid  --关联条件
    and B.Flag = 'A' and C.Code='1'  --更新条件
    --and len(b.Account)<=6  --不足6位的才更新  --***不加这个条件就行了
      

  18.   

    --当然,按我上面的,如果超过6位,则会截取6位,如果不截取,则再改为:update B set Account=rtrim(a.Id)+case when len(b.Account)<6 then right('000000'+rtrim(b.Account),6) else rtrim(b.Account) end
    from A,B,C
    where a.Account=b.Account and b.Uid=c.Uid  --关联条件
    and B.Flag = 'A' and C.Code='1'  --更新条件
      

  19.   

    --如果只处理<=6位的,则条件改一下就行了update B set Account=rtrim(a.Id)+right('000000'+rtrim(b.Account),6)
    from A,B,C
    where a.Account=b.Account and b.Uid=c.Uid  --关联条件
    and B.Flag = 'A' and C.Code='1'  --更新条件
    and len(b.Account)<=6  --<=6位的才更新