表示例如下:表名为 tblcol1  col2  col3山东  济南  1辽宁  沈阳  2山东  济南  3
......想实现如下功能:update tbl set col3 = '11' where col1 = '山东' and col2 = '济南';
update tbl set cold = '22' where col1 = '辽宁' and col2 = '沈阳';上述功能 能否用一条update语句来写?请指教。

解决方案 »

  1.   

    case when 
    还不如用2条呢
      

  2.   

    update 本来就是要分多条执行..
      

  3.   

    create proc p
    as 
     begin
         update tbl set col3 = '11' where col1 = '山东' and col2 = '济南';
         update tbl set cold = '22' where col1 = '辽宁' and col2 = '沈阳';
    endexec p
      

  4.   

    update #tb
    set col3=case when  col1 = '山东' and col2 = '济南' then '11'
      when  col1 = '辽宁' and col2 = '沈阳' then '22' 
      else col3 end还是这个就扫描一回表
      

  5.   

    update #tb set 
        col3=case when  col1 = '山东' and col2 = '济南' then '11'
                  when  col1 = '辽宁' and col2 = '沈阳' then '22' 
              end
    where (col1 = '山东' and col2 = '济南' ) or (col1 = '辽宁' and col2 = '沈阳' )
    cold要为col3,可以这样。
      

  6.   

    就两条一起执行呀如果是程序中要用到。。你在sql的string里直接把俩放在一起就可以了。。
      

  7.   


    一句就行啦

       UPDATE tbl SET col3 = col3 + col3*10
      

  8.   

    create table tbl(col1 varchar(10),col2 varchar(10),col3 varchar(10))
    insert into tbl values('山东', '济南', '1')
    insert into tbl values('辽宁', '沈阳', '2')
    insert into tbl values('山东', '济南', '3')
    goupdate tbl
    set col3 = (case when col1 = '山东' and col2 = '济南' then '11' 
                     when col1 = '辽宁' and col2 = '沈阳' then '22'
                     else col3 
                end)select * from tbldrop table tbl/*
    col1       col2       col3       
    ---------- ---------- ---------- 
    山东         济南         11
    辽宁         沈阳         22
    山东         济南         11(所影响的行数为 3 行)*/
      

  9.   

    可以用  case
    不过不如用2句
    update tbl set col3 = '11' where col1 = '山东' and col2 = '济南';update tb1 set col3=case when col1 = '山东' and col2 = '济南' then '11'
                       when col1 = '辽宁' and col2 = '沈阳' then '22' else '' end
    where col1 = '辽宁' and col2 = '沈阳' or col1 = '山东' and col2 = '济南'
    如果只是1改为11,2改为22,3改为33
    就用#13楼的方法,不过需转为字符型.