我有两个表,一个表是省份标准表,另一个是信息表,其中信息表里面的省份字段与省份标准表不同,
信息表省份字段是
如:省份 城市
广东 广州
北京 昌平
湖南 长沙标准表是
省份 城市
广东省 广州市
北京市 昌平区
湖南省 长沙市我想要两张表进行匹配,同时更新信息表中省份字段。之前那个问题多谢各位了,现在又要麻烦大家了
信息表省份字段是
如:省份 城市
广东 广州
北京 昌平
湖南 长沙标准表是
省份 城市
广东省 广州市
北京市 昌平区
湖南省 长沙市我想要两张表进行匹配,同时更新信息表中省份字段。之前那个问题多谢各位了,现在又要麻烦大家了
from 信息表 a,标准表 b
where patindex('%'+a.省份+'%',b.省份)>0
--如果以城市更新則 where patindex('%'+a.城市+'%',b.城市)>0
-- Author: T.O.P
-- Create date: 2009/12/01
-- Version: SQL SERVER 2005
-- =============================================
declare @tb1 table([省份] varchar(14),[城市] varchar(4))
insert @tb1
select '广东','广州' union all
select '北京','昌平' union all
select '湖南','长沙'declare @tb2 table([省份] varchar(6),[城市] varchar(6))
insert @tb2
select '广东省','广州市' union all
select '北京市','昌平区' union all
select '湖南省','长沙市'update a
set a.[省份] = b.[省份]
from @tb1 a , @tb2 b
where charindex(a.[省份],b.[省份])>0select * from @tb1
--测试结果:
/*
省份 城市
-------------- ----
广东省 广州
北京市 昌平
湖南省 长沙(3 row(s) affected)
*/
--> Author : js_szy
--> Target : 各位大大,小卒就是想要一朵花
--> Date : 2009-12-01 15:07:53
--> Version: SQL Server 2005
---------------------------------------------
--> 测试数据: @ta
declare @ta table (省份 varchar(14),城市 varchar(14))
insert into @ta
select '广东','广州' union all
select '北京','昌平' union all
select '湖南','长沙'
--> 测试数据: @tb
declare @tb table (省份 varchar(6),城市 varchar(6))
insert into @tb
select '广东省','广州市' union all
select '北京市','昌平区' union all
select '湖南省','长沙市'update @ta
set 省份=b.省份,城市=b.城市
from @ta a,@tb b
where charindex(a.省份 ,b.省份 )>0 and charindex(a.城市,b.城市)>0
select * from @ta省份 城市
-------------- --------------
广东省 广州市
北京市 昌平区
湖南省 长沙市(3 行受影响)
declare @table1 table([省份] varchar(6),[城市] varchar(6))
insert @table1
select '广东','广州' union all
select '北京','昌平' union all
select '湖南','长沙'
--> 测试数据:@table2
declare @table2 table([省份] varchar(6),[城市] varchar(6))
insert @table2
select '广东省','广州市' union all
select '北京市','昌平区' union all
select '湖南省','长沙市'update @table1 set 省份 = t.省份,城市 = t.城市
from @table1 r,@table2 t
where charindex(r.省份,t.省份) > 0select * from @table1
---------------------
广东省 广州市
北京市 昌平区
湖南省 长沙市
insert tb1
select '广东','广州' union all
select '北京','昌平' union all
select '湖南','长沙'create table tb2([省份] varchar(6),[城市] varchar(6))
insert tb2
select '广东省','广州市' union all
select '北京市','昌平区' union all
select '湖南省','长沙市'
go--查询
select
省份 = (select top 1 省份 from tb2 where charindex(tb1.省份 , tb2.省份) > 0),
城市 = (select top 1 城市 from tb2 where charindex(tb1.城市 , tb2.城市) > 0)
from tb1
/*
省份 城市
------ ------
广东省 广州市
北京市 昌平区
湖南省 长沙市(所影响的行数为 3 行)
*/--更新
update tb1
set
省份 = (select top 1 省份 from tb2 where charindex(tb1.省份 , tb2.省份) > 0),
城市 = (select top 1 城市 from tb2 where charindex(tb1.城市 , tb2.城市) > 0)
from tb1
select * from tb1
/*
省份 城市
-------------- ----------
广东省 广州市
北京市 昌平区
湖南省 长沙市(所影响的行数为 3 行)
*/
drop table tb1 , tb2
SET [省份] = [标准表].[省份],
[城市] = [标准表].[城市]
FROM [信息表]
JOIN [标准表] ON CHARINDEX([信息表].[省份], [标准表].[省份]) > 0
AND CHARINDEX([信息表].[城市], [标准表].[城市]) > 0
set
省份 = (select top 1 省份 from tb2 where tb1.省份 <> tb2.省份 and charindex(tb1.省份 , tb2.省份) > 0),
城市 = (select top 1 城市 from tb2 where tb1.城市 <> tb2.城市 and charindex(tb1.城市 , tb2.城市) > 0)
from tb1