表示例如下:表名为 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语句来写?请指教。
......想实现如下功能:update tbl set col3 = '11' where col1 = '山东' and col2 = '济南';
update tbl set cold = '22' where col1 = '辽宁' and col2 = '沈阳';上述功能 能否用一条update语句来写?请指教。
还不如用2条呢
as
begin
update tbl set col3 = '11' where col1 = '山东' and col2 = '济南';
update tbl set cold = '22' where col1 = '辽宁' and col2 = '沈阳';
endexec p
set col3=case when col1 = '山东' and col2 = '济南' then '11'
when col1 = '辽宁' and col2 = '沈阳' then '22'
else col3 end还是这个就扫描一回表
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,可以这样。
一句就行啦
UPDATE tbl SET col3 = col3 + col3*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 行)*/
不过不如用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楼的方法,不过需转为字符型.