日期:2014-05-18 浏览次数:20506 次
--第一个问题: create table A(JD_DM varchar(10),JF_DM varchar(10),ZD_ZH int,ZD_FH int,OLD_ZDBH varchar(20),zd_id int) insert into A values('003','011',21,3,'03011021-03',1) insert into A values('003','012',3 ,6,'03012003-06',2) insert into A values('003','018',9 ,0,'03018009' ,3) insert into A values('001','089',83,0,'01089083' ,4) insert into A values('001','089',21,2,'01089021-02',5) create table B(old varchar(20),new varchar(20),id int) insert into B values('03011021-03','03011021-03',null) insert into B values('03012003-06','03012003-08',null) insert into B values('03018009' ,'03018009' ,null) insert into B values('01089083' ,'01089083-01',null) insert into B values('01089021-02','01089021-02',null) go update B set id = t.zd_id from B, ( select right(JD_DM,2) JD_DM, JF_DM, right('000'+cast(ZD_ZH as varchar(3)),3) ZD_ZH, ZD_FH = case zd_fh when 0 then '' else right('00'+cast(zd_fh as varchar(2)),2) end, OLD_ZDBH,zd_id from A ) t where b.old = t.OLD_ZDBH and b.new = t.jd_dm + t.jf_dm + t.zd_zh + '-' + t.zd_fh select * from b drop table a,b /* old new id -------------------- -------------------- ----------- 03011021-03 03011021-03 1 03012003-06 03012003-08 NULL 03018009 03018009 NULL 01089083 01089083-01 NULL 01089021-02 01089021-02 5 (所影响的行数为 5 行) */