问题是这样的
比如:
1个表叫aa有字段
id title
1   你好
2   时间
3   解放
4   浪费
5   改变
第2个表 ss 有字段
id body
1 <img  src="/uppic/1.jpg" /><img src="/uppic/2.jpg" /><img src="/uppic/3.jpg" />
2 <img  src="/uppic/1.jpg" /><img src="/uppic/2.jpg" /><img src="/uppic/3.jpg" />
3 <img src="/uppic/1.jpg" /><img src="/uppic/2.jpg" />
4 <img src="/uppic/1.jpg" /><img src="/uppic/2.jpg" />
5 <img src="/uppic/1.jpg" />
现在要把每个body出现的img替换成
1 <img alt="你好" src="/uppic/1.jpg" /><img alt="你好" src="/uppic/2.jpg" /><img alt="你好"src="/uppic/3.jpg" />
2 <img alt="时间" src="/uppic/1.jpg" /><img src="/uppic/2.jpg" /><img alt="时间" src="/uppic/3.jpg" />
3 <img alt="解放" src="/uppic/1.jpg" /><img alt="解放" src="/uppic/2.jpg" />
4 <img alt="浪费" src="/uppic/1.jpg" /><img alt="解放" src="/uppic/2.jpg" />
5 <img alt="改变" src="/uppic/1.jpg" />SQL语句要怎么写??高手指点下!

解决方案 »

  1.   

    update tb2 set body = replace(body , 'img' , 'img alt = "' + a.title + '"') from tb2 , tb1 where tb2.id = tb1.id
      

  2.   

    create table tb1(id int , title  varchar(10))
    insert into tb1 values(1 , '你好') 
    insert into tb1 values(2 , '时间') 
    insert into tb1 values(3 , '解放') 
    insert into tb1 values(4 , '浪费') 
    insert into tb1 values(5 , '改变') 
    create table tb2(id int , body varchar(200))
    insert into tb2 values(1, '<img  src="/uppic/1.jpg" /> <img src="/uppic/2.jpg" /> <img src="/uppic/3.jpg" /> ')
    insert into tb2 values(2, '<img  src="/uppic/1.jpg" /> <img src="/uppic/2.jpg" /> <img src="/uppic/3.jpg" />') 
    insert into tb2 values(3, '<img src="/uppic/1.jpg" /> <img src="/uppic/2.jpg" /> ')
    insert into tb2 values(4, '<img src="/uppic/1.jpg" /> <img src="/uppic/2.jpg" />') 
    insert into tb2 values(5, '<img src="/uppic/1.jpg" /> ')
    goupdate tb2 set body = replace(body , 'img' , 'img alt = "' + tb1.title + '"') from tb2 , tb1 where tb2.id = tb1.idselect * from tb2drop table tb1 , tb2/*
    id          body                                                                                                                                                                                                     
    ----------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
    1           <img alt = "你好"  src="/uppic/1.jpg" /> <img alt = "你好" src="/uppic/2.jpg" /> <img alt = "你好" src="/uppic/3.jpg" /> 
    2           <img alt = "时间"  src="/uppic/1.jpg" /> <img alt = "时间" src="/uppic/2.jpg" /> <img alt = "时间" src="/uppic/3.jpg" />
    3           <img alt = "解放" src="/uppic/1.jpg" /> <img alt = "解放" src="/uppic/2.jpg" /> 
    4           <img alt = "浪费" src="/uppic/1.jpg" /> <img alt = "浪费" src="/uppic/2.jpg" />
    5           <img alt = "改变" src="/uppic/1.jpg" /> (所影响的行数为 5 行)*/
      

  3.   

    if object_id('ta')is not null drop table ta
    go
    create table ta(id int, title nvarchar(10)) 
    insert ta select 1,  N'你好' 
    insert ta select 2,  N'时间' 
    insert ta select 3,  N'解放' 
    insert ta select 4,  N'浪费' 
    insert ta select 5,  N'改变' 
    if object_id('tB')is not null drop table tB
    go
    CREATE TABLE TB(id INT, body NVARCHAR(200)) 
    INSERT TB SELECT 1 ,'<img  src="/uppic/1.jpg" /> <img src="/uppic/2.jpg" /> <img src="/uppic/3.jpg" /> '
    INSERT TB SELECT 2 ,'<img  src="/uppic/1.jpg" /> <img src="/uppic/2.jpg" /> <img src="/uppic/3.jpg" /> '
    INSERT TB SELECT 3 ,'<img src="/uppic/1.jpg" /> <img src="/uppic/2.jpg" />' 
    INSERT TB SELECT 4 ,'<img src="/uppic/1.jpg" /> <img src="/uppic/2.jpg" />' 
    INSERT TB SELECT 5, '<img src="/uppic/1.jpg" />' 
    update tb set body = replace(body , 'img' , 'img alt = "' + a.title + '"') from ta a  where tb.id = a.id
    select * from tb
    /*id          body                                                                                                                                                                                                     
    ----------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
    1           <img alt = "你好"  src="/uppic/1.jpg" /> <img alt = "你好" src="/uppic/2.jpg" /> <img alt = "你好" src="/uppic/3.jpg" /> 
    2           <img alt = "时间"  src="/uppic/1.jpg" /> <img alt = "时间" src="/uppic/2.jpg" /> <img alt = "时间" src="/uppic/3.jpg" /> 
    3           <img alt = "解放" src="/uppic/1.jpg" /> <img alt = "解放" src="/uppic/2.jpg" />
    4           <img alt = "浪费" src="/uppic/1.jpg" /> <img alt = "浪费" src="/uppic/2.jpg" />
    5           <img alt = "改变" src="/uppic/1.jpg" />*/
      

  4.   

    if object_id('ta')is not null drop table ta
    go
    create table ta(id int, title nvarchar(10)) 
    insert ta select 1,  N'你好' 
    insert ta select 2,  N'时间' 
    insert ta select 3,  N'解放' 
    insert ta select 4,  N'浪费' 
    insert ta select 5,  N'改变' 
    if object_id('tB')is not null drop table tB
    go
    CREATE TABLE TB(id INT, body NVARCHAR(200)) 
    INSERT TB SELECT 1 ,'<img  src="/uppic/1.jpg" /> <img src="/uppic/2.jpg" /> <img src="/uppic/3.jpg" /> '
    INSERT TB SELECT 2 ,'<img  src="/uppic/1.jpg" /> <img src="/uppic/2.jpg" /> <img src="/uppic/3.jpg" /> '
    INSERT TB SELECT 3 ,'<img src="/uppic/1.jpg" /> <img src="/uppic/2.jpg" />' 
    INSERT TB SELECT 4 ,'<img src="/uppic/1.jpg" /> <img src="/uppic/2.jpg" />' 
    INSERT TB SELECT 5, '<img src="/uppic/1.jpg" />' 
    update tb set body = replace(body , 'img' , 'img alt = "' + a.title + '"') from ta a  where tb.id = a.id
    select * from tb
    /*id          body                                                                                                                                                                                                     
    ----------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
    1           <img alt = "你好"  src="/uppic/1.jpg" /> <img alt = "你好" src="/uppic/2.jpg" /> <img alt = "你好" src="/uppic/3.jpg" /> 
    2           <img alt = "时间"  src="/uppic/1.jpg" /> <img alt = "时间" src="/uppic/2.jpg" /> <img alt = "时间" src="/uppic/3.jpg" /> 
    3           <img alt = "解放" src="/uppic/1.jpg" /> <img alt = "解放" src="/uppic/2.jpg" />
    4           <img alt = "浪费" src="/uppic/1.jpg" /> <img alt = "浪费" src="/uppic/2.jpg" />
    5           <img alt = "改变" src="/uppic/1.jpg" />*/