将行从Table1移动到Table2(存档),并在Table1中保留7天的数据

时间:2017-05-07 12:31:52

标签: sql sql-server tsql move archive

Table1包含包含创建日期的列。我想归档早于过去7天的行,我希望将这些行移到新表Table2中。 如何编写将存档数据更新到表2中的脚本,并仅保留Table1中过去7天的数据。谢谢!

3 个答案:

答案 0 :(得分:0)

我倾向于做这样的事情:

declare @cutoff_datetime datetime;
set @cutoff_datetime = dateadd(day, -7, getdate());

insert into table2 ( . . . )
    select . . .
    from table1
    where createdate < @cutoff_datetime;

delete from table1
    where createdate < @cutoff_datetime;

在以下两种情况下,这是交易安全的:

  • table1中的行未更新;和,
  • 使用当前时间插入新行。

如果可以更新行,您应该向另一个问题询问有关您想要做什么的更详细的规范。

答案 1 :(得分:0)

这使用datadd()datediff()来获取7天前的开始日期,并在7天前删除table1行;使用output将已删除的行插入table2

insert into table2 (col1, col2, some_date)
select d.col1, d.col2, d.some_date
from (
  delete 
  from table1
  output deleted.col1, deleted.col2, deleted.some_date
  where some_date <  dateadd(day, datediff(day, 0, getdate() -7), 0)
  ) d (col1, col2, some_date)

Rextester演示:http://rextester.com/DNK69844

create table table1 (col1 int not null identity(2,1), col2 varchar(32), some_date date);
insert into table1 values ('Eight','20170429'),('Seven','20170430'),('Six','20170501');

create table table2 (col1 int not null, col2 varchar(32), some_date date);
insert into table2 values (1,'Nine','20170428');

select tbl='table1', col1, col2, some_date=convert(char(10),some_date,120) from table1
union all
select tbl='table2', col1, col2, some_date=convert(char(10),some_date,120) from table2;

insert into table2 (col1, col2, some_date)
select d.col1, d.col2, d.some_date
from (
  delete 
  from table1
  output deleted.col1, deleted.col2, deleted.some_date
  where some_date <  dateadd(day, datediff(day, 0, getdate() -7), 0)
  ) d (col1, col2, some_date);


select tbl='table1', col1, col2, some_date=convert(char(10),some_date,120) from table1
union all
select tbl='table2', col1, col2, some_date=convert(char(10),some_date,120) from table2;

返回(之前):

+--------+------+-------+------------+
|  tbl   | col1 | col2  | some_date  |
+--------+------+-------+------------+
| table1 |    2 | Eight | 2017-04-29 |
| table1 |    3 | Seven | 2017-04-30 |
| table1 |    4 | Six   | 2017-05-01 |
| table2 |    1 | Nine  | 2017-04-28 |
+--------+------+-------+------------+

返回(之后):

+--------+------+-------+------------+
|  tbl   | col1 | col2  | some_date  |
+--------+------+-------+------------+
| table1 |    3 | Seven | 2017-04-30 |
| table1 |    4 | Six   | 2017-05-01 |
| table2 |    1 | Nine  | 2017-04-28 |
| table2 |    2 | Eight | 2017-04-29 |
+--------+------+-------+------------+

答案 2 :(得分:0)

- 创建临时表

CREATE TABLE #temp (PK_Col INT);  -- Only to store the Primary Key values of deleted rows.
GO

- 将行插入存档表

INSERT INTO Archive.Table (PK_Col , created_date , OtherCols,.....)
OUTPUT inserted.PK_Col INTO #Temp
SELECT PK_Col , created_date , OtherCols,.....
FROM Table1
WHERE created_date < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-7), 0)
GO

- 现在删除存档数据

DELETE FROM t
FROM Table1 t
WHERE EXISTS ( SELECT 1 from #temp t1 WHERE t1.PK_Col = t.PK_Col)
GO