今天遇到一个问题,要批量拆分处理几十万条电话号码,将电话拆分为单个组成一个电话列表,原本计划用Delphi连接数据库来进行了处理,但是后来考虑到处理效率,还是用SQL语句来处理比较好,于是首先将电话列表整理好,每个用户的电话用/分隔,电话在一个字段里,类似A/B/C,把不规则的号码整理后,然后准备拆分函数,将A/B/C拆分为A,B,C三个号码,分别写入电话列表库,最近使用游标循环对几十万用户的电话号码进行批理处理,30万用户电话,只花了7分43秒就搞定了,拆分后共计81万个电话号码,哈哈,SQL游标做循环批量操作太爽了,外加存储过程,加函数配合,基本上能实现数据整理,写入,匹配操作。两个字总结:强大。
游标批量处理
CREATE PROCEDURE AutoGetTelList AS
declare @UserID int
declare @mytelstr varchar(200)
declare cur_getUserID cursor for
select main_id from aipu_main where main_id>=10000 and main_id<20000
open cur_getUserID
fetch next from cur_getUserID into @UserID
while(@@fetch_status = 0)
begin
select @mytelstr=联系方式 from aipu_main where main_id=@UserID
exec SamoolSplitTel @mytelstr,'/',@UserID
fetch next from cur_getUserID into @UserID
end
close cur_getUserID
deallocate cur_getUserID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
SQL拆分函数
CREATE Procedure SamoolSplitTel
(@origStr varchar(7000),
@markStr varchar(100),
@main_id varchar(20)
)
AS
BEGIN
declare @strlen int,@postion int,@start int,@sublen int,
@TEMPstr varchar(200),@TEMPid int
SELECT @strlen=LEN(@origStr),@start=1,@sublen=0,@postion=1,@TEMPstr='',@TEMPid=0
if(RIGHT(@origStr,1)<>@markStr )
begin
set @origStr = @origStr + @markStr
end
WHILE((@postion<=@strlen) and (@postion !=0))
BEGIN
IF(CHARINDEX(@markStr,@origStr,@postion)!=0)
BEGIN
SET @sublen=CHARINDEX(@markStr,@origStr,@postion)-@postion;
END
ELSE
BEGIN
SET @sublen=@strlen-@postion+1;
END
IF(@postion<=@strlen)
BEGIN
SET @TEMPid=@TEMPid+1;
SET @TEMPstr=SUBSTRING(@origStr,@postion,@sublen);
INSERT INTO aipu_tellist(main_id,tel) values(@main_id,@TEMPstr)
IF(CHARINDEX(@markStr,@origStr,@postion)!=0)
BEGIN
SET @postion=CHARINDEX(@markStr,@origStr,@postion)+1
END
ELSE
BEGIN
SET @postion=@postion+1
END
END
END
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO