去重 存储过程

见如下代码:是多表操作的。

1、存储过程里可以写临时表的,但这里用的是exec语句创建的临时表。同时存储过程里不能写临时表的表名,因为临时表是动态创建的,当时并不存在,所以存储过程会报错。

2、注意到:这里的临时表用完后不用drop,它是自动删除的。

3、exec里面好像只能执行一个命令,就比如说你有很多个select,delete啊那就不行了。所以只能一条一条来。

4、存储过程里不能有go.否则go后面的语句将被抛弃。

5、exex里面执行的语句串是用单引号和加号连接的。

 

create proc [dbo].[clearDupdata] 
@interDay bigint 
as

--table1-----------------------------------------------------------------------------------------------------------------
exec ('select distinct * into #tmpActivity from test.dbo.dActivityCodeStat where 1=2')
exec ('select distinct * into #tmpAgentApp from test.dbo.dAgentByApplicationStat where 1=2')
exec ('select distinct * into #tmpPerform from test.dbo.dAgentPerformanceStat  where 1=2')
exec ('select distinct * into #tmpAppStat from test.dbo.dApplicationStat  where 1=2')
exec ('select distinct * into #tmpSkillsetStat from test.dbo.dSkillsetStat  where 1=2')
exec ('select distinct * into #tmpAgentLogin from test.dbo.eAgentLoginStat where 1=2')
select distinct * into #tmpActivity from test.dbo.dActivityCodeStat where datediff(dd,timestamp,getdate())=@interDay
delete from test.dbo.dActivityCodeStat where datediff(dd,timestamp,getdate())=@interDay
insert into test.dbo.dActivityCodeStat select  * from #tmpActivity


--table2-----------------------------------------------------------------------------------------------------------------


select distinct * into #tmpAgentApp from test.dbo.dAgentByApplicationStat where datediff(dd,timestamp,getdate())=@interDay 
delete from test.dbo.dAgentByApplicationStat where datediff(dd,timestamp,getdate())=@interDay
insert into test.dbo.dAgentByApplicationStat select  * from #tmpAgentApp


--table3-----------------------------------------------------------------------------------------------------------------

select distinct * into #tmpPerform from test.dbo.dAgentPerformanceStat where datediff(dd,timestamp,getdate())=@interDay
delete from test.dbo.dAgentPerformanceStat where datediff(dd,timestamp,getdate())=@interDay
insert into test.dbo.dAgentPerformanceStat select  * from #tmpPerform

--table4-----------------------------------------------------------------------------------------------------------------

select distinct * into #tmpAppStat from test.dbo.dApplicationStat where datediff(dd,timestamp,getdate())=@interDay 
delete from test.dbo.dApplicationStat where datediff(dd,timestamp,getdate())=@interDay
insert into test.dbo.dApplicationStat select  * from #tmpAppStat

--table5-----------------------------------------------------------------------------------------------------------------
select distinct * into #tmpSkillsetStat from test.dbo.dSkillsetStat where datediff(dd,timestamp,getdate())=@interDay 
delete from test.dbo.dSkillsetStat where datediff(dd,timestamp,getdate())=@interDay
insert into test.dbo.dSkillsetStat select  * from #tmpSkillsetStat

--table6-----------------------------------------------------------------------------------------------------------------

select distinct * into #tmpAgentLogin from test.dbo.eAgentLoginStat where datediff(dd,timestamp,getdate())=@interDay 
delete from test.dbo.eAgentLoginStat where datediff(dd,timestamp,getdate())=@interDay
insert into test.dbo.eAgentLoginStat select  * from #tmpAgentLogin
 

 

 

在执行时,用如下命令:

 

 

exec clearDupdata 6
 

refurl:http://topic.csdn.net/t/20021125/12/1202902.html

http://zhidao.baidu.com/question/230958004.html?seed=0

http://topic.csdn.net/u/20080122/16/58dd3f13-652e-4161-b872-3486820bfdaa.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值