有这样一段代码.是为了将每个月的post按照dateofpost从post表里归类到各个月的post_200***表里的(以本月为例).
select * into temp_MovePost from post  where dateofpost>='2006-08-01' and dateofpost<'2006-09-01'
go
delete a from post a left join temp_MovePost b on a.refindkey = b.refindkey where b.refindkey is not null
go
delete a from temp_MovePost a left join post_200608 b on a.refindkey = b.refindkey where b.refindkey is not null
go 
insert into post_200608(refindkey,firstextractiondate,latestextractiondate,threadid,subject,content,dateofpost,itemurl,poster,siteid,forumid,istopicpost)
select refindkey,firstextractiondate,latestextractiondate,threadid,subject,content,dateofpost,itemurl,poster,siteid,forumid,istopicpost from temp_MovePost
go
drop table temp_MovePost
go请求存储过程

解决方案 »

  1.   

    CREATE PROC dbo.p_MovePost
    AS
    DECLARE @dt_begin datetime, @dt_end datetime,
    @tbname sysname,
    @s nvarchar(4000)
    SELECT 
    @dt_begin = DATEADD(Day, 1 - DAY(GETDATE()), CONVERT(char(10), GETDATE(), 120)),
    @dt_end = DATEADD(Month, 1, @dt_begin),
    @tbname = QUOTENAME('post_' + CONVERT(char(6), @dt_begin, 112))select * into temp_MovePost from post  
    where dateofpost>=@dt_begin and dateofpost<@dt_begin
    delete a from post a left join temp_MovePost b on a.refindkey = b.refindkey where b.refindkey is not null
    set @s = N'
    delete a from temp_MovePost a left join ' + @tbname+ N' b on a.refindkey = b.refindkey where b.refindkey is not null
    insert into ' + @tbname+ N'(refindkey,firstextractiondate,latestextractiondate,threadid,subject,content,dateofpost,itemurl,poster,siteid,forumid,istopicpost)
    select refindkey,firstextractiondate,latestextractiondate,threadid,subject,content,dateofpost,itemurl,poster,siteid,forumid,istopicpost from temp_MovePost'
    exec sp_executesql @s, N'@dt_begin datetime, @dt_end datetime', @dt_begin , @dt_end 
    drop table temp_MovePost
    GO