表1具有包括创建日期的列。我想归档比过去7天更早的行,我希望将这些行移动到新的表格Table2中。 如何编写将更新归档数据到表2中的脚本,并且仅保留表1中过去7天的数据。谢谢!将表1中的行移动到表2(存档),并将表1中的数据保留过去7天
-1
A
回答
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
如果行可以被更新,你应该问另一个问题有更详细的规格你想要做什么。
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 |
+--------+------+-------+------------+
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
相关问题
- 1. 根据表#1中列的值将表数据从表#1复制到表#2
- 2. 将数据从工作表1移动到工作表2
- 3. 合并2表中的SQL,并保存到1个新表
- 4. vaadin:从表1的选择行移动数据参见表2
- 5. WinForms - 将数据绑定到1表格上的2个表格并保存到数据库
- 6. 查找表2中的数据并将其显示在表1中
- 7. 2数据表1
- 8. 将表A中的行移入表中 - 存档
- 9. 如何在sqlCE中将2个表合并为1个表?
- 10. 从工作表中找到一个值并将工作表1中的行复制到工作表2
- 11. Webservice方法通用列表填充并保留1天
- 12. 如何将数据插入其他2个表中的1个表中?
- 13. 将3个VBA宏组合到1中(2个删除不同的表中的行,1个刷新数据)
- 14. 将DataTable保存到数据库表中
- 15. 将Sharepoint数据保存到SQLite表中
- 16. 将字符串中表示的大数除以2,加1或减1 1
- 17. 将MySQL表中的数据保存到另一个表中
- 18. 如何将表格中的数据保存/插入到表中?
- 19. 将数据插入到表2天
- 20. 的MySQL从表1中,其中表2
- 21. 如何将数据从天蓝色的TABLESTORAGE tproduction表移动到开发表中?
- 22. 在Windows 7上将数据保存到注册表中
- 23. Drupal 7将表单保存到数据库中
- 24. 如何将表1中的列添加为表2的类型?
- 25. 计算表1中的实例并链接到表2
- 26. 将表格行数据移动到html中的另一行
- 27. MySQL将1行以上的行移动到另一个表中,全部在1条语句中?
- 28. 将xml文档保存到数据表中
- 29. 如何将Flex表单数据保存到Word文档中?
- 30. 数据库模式,1表或2表
您的代码?你困在哪里? –
INSERT INTO Table2(columns .....)从Table2中选择(列...)其中createddate> = DATEADD(day,-7,GETDATE())从Table1中删除where where createddate> = DATEADD(day,-7, GETDATE()) – muddu83