日期:2014-05-16 浏览次数:20423 次
--建立清洗函数
CREATE FUNCTION FUN_MU(@STR VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
SELECT @STR=REPLACE(@STR,F2,'')
FROM T2
RETURN @STR
END
GO
--更新F1列
UPDATE T1 SET F1=DBO.FUN_MU(F1)
--插入数据时用函数替代原列
INSERT INTO T1(F1)
VALUES(DBO.FUN_MU('TEST'))
--确保过滤表中行数别太多,不然插入速度会变慢
create table TALBE1(F1 varchar(50))
create table T2(F2 varchar(5))
insert into TALBE1(F1)
select '123
------解决方案--------------------
=456' union all
select '123()456' union all
select '123=!456'
insert into T2(F2)
select '
------解决方案--------------------
' union all
select '='
-- 清洗前
select F1 from TALBE1
/*
F1
------------------------
123
------解决方案--------------------
=456
123()456
123=!456
(3 row(s) affected)
*/
-- 清洗
while(exists(select 1
from TALBE1 a
cross join T2 b
where charindex(b.F2,a.F1,1)>0) )
begin
update a
set a.F1=replace(a.F1,b.F2,'')
from TALBE1 a
cross join T2 b
where charindex(b.F2,a.F1,1)>0
end
-- 清洗后
select F1 from TALBE1
/*
F1
-----------------------
123456
123()456
123!456
(3 row(s) affected)
*/