----创建测试数据
declare @ta table(fNO varchar(10),fName varchar(10))
insert @ta
select '01',          '河北省'   union all 
select '01.01',       '石家庄市' union all  
select '01.01.01',    '长安区'   union all 
select '01.01.02',    '桥东区'   union all
select '01.01.03',    '桥西区'   union all
select '01.01.04',    '新华区'
declare @tb table(fAA varchar(10),fBB varchar(30))
insert @tb
select null,'遵化市侯家寨乡' union all
select null,'天津市大港区春晖里' union all
select null,'河北省石家庄新华区大安路'----查询
select d.fNO,b.fBB from @tb as b 
inner join @ta as a on len(a.fNO) = 2 and charindex(replace(replace(a.fName,'省',''),'市',''),b.fBB) > 0
inner join @ta as c on len(c.fNO) = 5 and charindex(replace(replace(c.fName,'省',''),'市',''),b.fBB) > 0
inner join @ta as d on len(d.fNO) = 8 and charindex(replace(replace(d.fName,'省',''),'市',''),b.fBB) > 0/*查询结果
fNO        fBB                            
---------- ------------------------------ 
01.01.04   河北省石家庄新华区大安路
*/
----更新
UPDATE b SET fAA = d.fNO
FROM @tb as b
inner join @ta as a on len(a.fNO) = 2 and charindex(replace(replace(a.fName,'省',''),'市',''),b.fBB) > 0
inner join @ta as c on len(c.fNO) = 5 and charindex(replace(replace(c.fName,'省',''),'市',''),b.fBB) > 0
inner join @ta as d on len(d.fNO) = 8 and charindex(replace(replace(d.fName,'省',''),'市',''),b.fBB) > 0
----查看
select * from @tb/*更新结果
fAA        fBB                            
---------- ------------------------------ 
NULL       遵化市侯家寨乡
NULL       天津市大港区春晖里
01.01.04   河北省石家庄新华区大安路
*/

解决方案 »

  1.   

    借用一两的数据
    declare @ta table(fNO varchar(10),fName varchar(10))
    insert @ta
    select '01',          '河北省'   union all 
    select '01.01',       '石家庄市' union all  
    select '01.01.01',    '长安区'   union all 
    select '01.01.02',    '桥东区'   union all
    select '01.01.03',    '桥西区'   union all
    select '01.01.04',    '新华区'declare @tb table(fAA varchar(10),fBB varchar(30))
    insert @tb
    select null,'遵化市侯家寨乡' union all
    select null,'天津市大港区春晖里' union all
    select null,'河北省石家庄新华区大安路'
    update @tb set fAA=t.fNO
    from
    (
    select a.fNO,b.fBB as fB from @ta a right join @tb b
    on b.fBB like '%'+a.fName+'%'
    where a.fNO is not null and len(a.fNO)=8
    )t
    where fBB=t.fBselect * from @tbfAA        fBB                            
    ---------- ------------------------------ 
    NULL       遵化市侯家寨乡
    NULL       天津市大港区春晖里
    01.01.04   河北省石家庄新华区大安路
      

  2.   

    发现问题了.我在二楼的回复和楼上bill024(咖啡熊)的回复都不能排除区或市重名的情况.例如使用下面的测试数据时(有二个同名的新华区),结果应该为:
    01.01.04   河北省石家庄新华区大安路
    但是运行结果却为:
    02.01.04   河北省石家庄新华区大安路测试数据:
    declare @ta table(fNO varchar(10),fName varchar(10))
    insert @ta
    select '02',          '河南省'   union all 
    select '02.01',       '郑州市'   union all  
    select '02.01.01',    '海淀区'   union all 
    select '02.01.02',    '西城区'   union all
    select '02.01.03',    '东城区'   union all
    select '02.01.04',    '新华区'   union all
    select '01',          '河北省'   union all 
    select '01.01',       '石家庄市' union all  
    select '01.01.01',    '长安区'   union all 
    select '01.01.02',    '桥东区'   union all
    select '01.01.03',    '桥西区'   union all
    select '01.01.04',    '新华区'  
    declare @tb table(fAA varchar(10),fBB varchar(30))
    insert @tb
    select null,'遵化市侯家寨乡' union all
    select null,'天津市大港区春晖里' union all
    select null,'河北省石家庄新华区大安路'
      

  3.   

    在二楼的回复就意在消除市区重名的情况,却仍有疏漏,这样修改一下:
    ----创建测试数据
    declare @ta table(fNO varchar(10),fName varchar(10))
    insert @ta
    select '02',          '河南省'   union all 
    select '02.01',       '郑州市'   union all  
    select '02.01.01',    '海淀区'   union all 
    select '02.01.02',    '西城区'   union all
    select '02.01.03',    '东城区'   union all
    select '02.01.04',    '新华区'   union all
    select '01',          '河北省'   union all 
    select '01.01',       '石家庄市' union all  
    select '01.01.01',    '长安区'   union all 
    select '01.01.02',    '桥东区'   union all
    select '01.01.03',    '桥西区'   union all
    select '01.01.04',    '新华区'  
    declare @tb table(fAA varchar(10),fBB varchar(30))
    insert @tb
    select null,'遵化市侯家寨乡' union all
    select null,'天津市大港区春晖里' union all
    select null,'河北省石家庄新华区大安路'----查询
    select *,a.fNO,b.fBB from @tb as b 
    inner join @ta as a on 
    len(a.fNO) = 8 and charindex(replace(replace(a.fName,'省',''),'市',''),b.fBB) > 0
    and exists(select 1 from @ta as t where len(fNo) = 5 and fNo = left(a.fNo,5) and charindex(replace(replace(t.fName,'省',''),'市',''),b.fBB) > 0)
    and exists(select 1 from @ta as t where len(fNo) = 2 and fNo = left(a.fNo,2) and charindex(replace(replace(t.fName,'省',''),'市',''),b.fBB) > 0)/*查询结果
    fNO        fBB                            
    ---------- ------------------------------ 
    01.01.04   河北省石家庄新华区大安路
    */----更新
    UPDATE b SET fAA = a.fNO
    FROM @tb as b
    inner join @ta as a on 
    len(a.fNO) = 8 and charindex(replace(replace(a.fName,'省',''),'市',''),b.fBB) > 0
    and exists(select 1 from @ta as t where len(fNo) = 5 and fNo = left(a.fNo,5) and charindex(replace(replace(t.fName,'省',''),'市',''),b.fBB) > 0)
    and exists(select 1 from @ta as t where len(fNo) = 2 and fNo = left(a.fNo,2) and charindex(replace(replace(t.fName,'省',''),'市',''),b.fBB) > 0)----查看
    select * from @tb/*更新结果
    fAA        fBB                            
    ---------- ------------------------------ 
    NULL       遵化市侯家寨乡
    NULL       天津市大港区春晖里
    01.01.04   河北省石家庄新华区大安路
    */
      

  4.   

    问题是有的fBB字段没有“省”“市”之类字啊
      

  5.   

    charindex(replace(replace(t.fName,'省',''),'市',''),b.fBB) > 0)
    ---------------------------------------------------------------------------
    这个语句就是用来应付楼主所说的情况的.使用这个语句处理之后,A表的fName中的"省"、"市"字样就被空字符''替换掉了,然后使用这样的fName去判断是否出现在了B表的fBB中.
    上面回复的测试代码中就已经解决了这个问题,例如:
    A表的测试数据中有一行:
    select '01.01',       '石家庄市' union all  /*处理后变成了'石家庄'*/
    B表的测试数据中有一行:
    select null,'河北省石家庄新华区大安路'
    经过处理之后,A表的 '石家庄市' 就变成了 '石家庄',而 '石家庄' 正好出现在了B表的fBB中.
    请楼主仔细体会一下上面的回复,或多写些数据测试一下.
      

  6.   

    还有一个最重要的问题就是如果用户填写的fBB字段比较有个性,实在没有办法匹配,也要显示出来,以便于知道哪些成功哪些没有成功~~
      

  7.   

    更新后:
    --查询编码更新成功的
    select * from @tb where fAA IS NOT NULL
    --查询编码更新不成功的
    select * from @tb where fAA IS NULL
      

  8.   

    还有个疑问老大,为什么同样的地址经过上面的处理后得到的编码并不相同?
    如:
    01.02.14 唐山市路北区西北井楼
    01.06.02 唐山市路北区西北井楼
    32.12.01 唐山市路北区西北井楼
    01.02.13 唐山市路南区南厂楼
    01.06.03 唐山市路南区南厂楼
    32.05.01 唐山市路南区南厂楼好像相当有规律都是这样
    我用的语句是:select a.fNO,b.f4019 from #ta as b 
    inner join tItem as a on 
    len(a.fNO) = 8 and charindex(replace(replace(replace(a.fName,'省',''),'市',''),'县',''),b.f4019) > 0
    and exists(select 1 from tItem as t where len(fNo) = 5 and fNo = left(a.fNo,5) and charindex(replace(replace(replace(a.fName,'省',''),'市',''),'县',''),b.f4019) > 0)
    and exists(select 1 from tItem as t where len(fNo) = 2 and fNo = left(a.fNo,2) and charindex(replace(replace(replace(a.fName,'省',''),'市',''),'县',''),b.f4019) > 0)         语句仍然不能处理如"路南区","南区",这样的类型