导入xls或者cvs数据并处理重复数据+关系表数据

来源:互联网 发布:b站网络视听许可证 编辑:程序博客网 时间:2024/05/16 23:10
 /*

开始处理前,请先检查数据是否存在问题。

如:数字出现 .0 或者1.3e+010等异常,说明导入工作中,存在类型不正确情况;请执行 drop table zygjqh$;  ;之后重新导入。

*/

select * from zygjqh$;

/*

在导入表中增加预留字段及判断标识*/

alter table zygjqh$ add client_id varchar(50);

alter table zygjqh$ add isNew varchar(50);

update zygjqh$ set isNew='1';

/*

根据呼叫中心系统数据库ACTIVESVC_ACTIVE表中产品信息与临时表zygjqh$表中的分组信息(即类型信息)进行对比,更新类型字段。

zygjqh$ 数据导入表

group_id 数据导入表中分组编号字段

LME库存 ACTIVESVC_ACTIVE表中新增的客户类型名称

group_name 数据导入表中分组名称字段

LME库存 数据导入表中分组名称

其他语句雷同

*/

update zygjqh$ set group_id=(select top 1 ID from ACTIVESVC_ACTIVE where ACTIVENAME = 'LME库存') where group_name = 'LME库存';

update zygjqh$ set group_id=(select top 1 ID from ACTIVESVC_ACTIVE where ACTIVENAME =

'正式IB组') where group_name = '正式IB组';

/*

删除导入文件中可能存在空字段的内容导入了临时表zygjqh$

user_name 数据导入表中客户姓名

*/

delete from zygjqh$ where user_name is null;

/*

将整理后的临时表zygjqh$增加标示符后 导入临时表#temp

*/

select   *,IDENTITY(int,1,1)   as   num   into   #temp   from   zygjqh$ ;

/*

针对客户对应多个组的情况,更改isNew标识,防止客户信息重复导入

*/

update  #temp set isNew='0' where num in(

select a.num from #temp  a,(select user_name,mobile from #temp group by user_name,mobile Having count (* )>1) b

where a.user_name = b.user_name and a.mobile = b.mobile

and a.num  not in (select min( num ) from #temp group by user_name,mobile Having count (* )>1));

/*

将临时表#temp中的数据 导入 呼叫中心数据表client表中,如果有其他字段请与client表的对应字段对照(实际操作中的其他数据字段请参照附件中的client表结构信息进行对照工作)

*/

insert into client(id,name,mobile,sex ,isactive)

select (select max(id)  from client)+num as id,user_name as name,mobile,sex,1 as isactive from #temp where isNew='1';

/*

将插入client表中的客户信息生成的id 写入临时表#temp

*/

update #temp  set client_id=client.id from #temp,client where #temp.user_name=client.name and #temp.mobile=client.mobile; 

/*

将客户id与产品id导入关系表ACTIVESVC_ACTIVETASK

*/

insert into ACTIVESVC_ACTIVETASK(CLIENTID,ACTIVEID,ORDERTIME)

select client_id as clientid,group_id as activeid,getdate() from #temp;

/*

更改client表的sequence值

*/

update sys_sequence set SEQUENCE_INDEX=(select (SEQUENCE_INDEX+(select count(*) from #temp where isNew='1')) as num from sys_sequence where SEQUENCE_NAME='CLIENT') where SEQUENCE_NAME='CLIENT';

/*

删除临时导入表zy_client,删除临时整理表#temp

*/

drop table zygjqh$;

drop table #temp;