读书人

求一条sqlserver的替换语句,该怎么处理

发布时间: 2012-01-31 21:28:41 作者: rapoo

求一条sqlserver的替换语句
有2个表,如下:
Table1:
类别 编码 解释
编码 1 中国
编码 2 法国
编码 3 德国
新编码 a 中国
新编码 b 法国
新编码 c 德国



Table2:
Expr1 Expr2
张三 1
李四 2
王五 3
马六 1


想把Table2的Expr2中的编码1、2、3换成新编码a、b、c。
多谢多谢!

[解决办法]

SQL code
--原始数据:@1declare @1 table(类别 varchar(6),编码 varchar(1),解释 varchar(4))insert @1select '编码','1','中国' union allselect '编码','2','法国' union allselect '编码','3','德国' union allselect '新编码','a','中国' union allselect '新编码','b','法国' union allselect '新编码','c','德国'--原始数据:@2declare @2 table(Expr1 varchar(4),Expr2 varchar(1))insert @2select '张三',1 union allselect '李四',2 union allselect '王五',3 union allselect '马六',1update a set a.Expr2=c.编码 from @2 a join @1 b on a.Expr2=b.编码 join @1 c on b.解释=c.解释 where b.类别='编码' and c.类别='新编码'select * from @2/*Expr1 Expr2 ----- ----- 张三    a李四    b王五    c马六    a*/ 

读书人网 >SQL Server

热点推荐