有表A
id     path
----------------
s1     qwerxio
s2     uipxkl
s3     hjlxkhf
s4     cvbmndfhu
s5     sdexyhn其中path字段是CHAR型,path中的每个字母最多只会在内容中出现一次
现需要更新s1,s3,s5三条记录的path字段,使其内容中的'x'字母前的内容更新为'12345','x'字母及其后的内容保持不变
即更新后,s1,s3,s5的path内容为:
id     path
----------------
s1     12345xio
s3     12345xkhf
s5     12345xyhn最坏的方法是分3条update语句,更新这三条记录
请问有没有更好的方法,可以提高update的效率呢? 包括在程序中进行字符串处理后在写入数据库的方法,只要效率高就可以了
急啊,请各位高手帮忙一下,非常谢谢!!!!!

解决方案 »

  1.   

    update A
    set path = '12345' + substring(path , charindex('x',path)+1 , len(path))
    where id in ('s1','s3','s5')
      

  2.   

    update 表 set [path] = stuff([path],1,charindex('x',[path])-1,'12345') where id in ('s1','s2','s3')
      

  3.   

    create table A (id varchar(10), path varchar(20))
    insert into A values('s1', 'qwerxio') 
    insert into A values('s2', 'uipxkl') 
    insert into A values('s3', 'hjlxkhf') 
    insert into A values('s4', 'cvbmndfhu') 
    insert into A values('s5', 'sdexyhn') 
    goupdate A
    set path = '12345' + substring(path , charindex('x',path)  , len(path))
    where id in ('s1','s3','s5')select * from Adrop table A/*
    id         path                 
    ---------- -------------------- 
    s1         12345xio
    s2         uipxkl
    s3         12345xkhf
    s4         cvbmndfhu
    s5         12345xyhn
    (所影响的行数为 5 行)
    */
      

  4.   

    --> 测试数据: @T
    declare @T table (id varchar(2),path varchar(9))
    insert into @T
    select 's1','qwerxio' union all
    select 's2','uipxkl' union all
    select 's3','hjlxkhf' union all
    select 's4','cvbmndfhu' union all
    select 's5','sdexyhn'update @T set [path] = stuff([path],1,charindex('x',[path])-1,'12345') where id in ('s1','s2','s3')
    -- OR 龟兄
    select * from @T
      

  5.   

    declare @tb table (id varchar(10),path varchar(20))
    insert into @tb select 's1','qwerxio'
    insert into @tb select 's2','uipxkl'
    insert into @tb select 's3','hjlxkhf'
    insert into @tb select 's4','cvbmndfhu'
    insert into @tb select 's5','sdexyhn'
    update t set path=case when charindex('x',path)>0 then stuff(path,1,charindex('x',path)-1,'12345') else path end
    from @tb t where right(id,1)%2<>0
    select * from @tbs1 12345xio
    s2 uipxkl
    s3 12345xkhf
    s4 cvbmndfhu
    s5 12345xyhn
      

  6.   

    declare @tb table (id varchar(10),path varchar(20))
    insert into @tb select 's1','qwerxio'
    insert into @tb select 's2','uipxkl'
    insert into @tb select 's3','hjlxkhf'
    insert into @tb select 's4','cvbmndfhu'
    insert into @tb select 's5','sdexyhn'update t set path=stuff(path,1,charindex('x',path)-1,'12345')
    from @tb t where right(id,1)%2<>0select * from @tbs1 12345xio
    s2 uipxkl
    s3 12345xkhf
    s4 cvbmndfhu
    s5 12345xyhn
      

  7.   

    update A
    set path = '12345' + substring(path , charindex('x',path)+1 , len(path))
    where id in ('s1','s3','s5')
      

  8.   

    select id,name,stuff(name,1,charindex('x',name)-1,'12345')
     from test where name like '%x%'
      

  9.   

    select id,path,stuff(path,1,charindex('x',path)-1,'12345')
     from a where path like '%x%'