如何删除 MS SQL 数据库中最早的一周(期间)?
How to delete oldest week(period) in MS SQL database?
MS SQL 2008R2 Express,我有数据库 - 一个 table 有行。当 db 大小变成大约 10gb 时,我需要在这个 table 中清理最早的一周(我不知道日期)。你能帮我写剧本吗?谢谢
UPD1。
有 DateAndTime 列,所以我在 VBA:
中进行这样的查询
strSQL = "SELECT DateAndTime
,TagName
,Val
,SetPoint
,Limit_H
,Limit_L
,Result
FROM dbo.Statistic
WHERE DateAndTime BETWEEN CAST('" & TimeBegin & "' AS datetime) AND CAST( '" & TimeEnd & " ' AS datetime)
Order By DateAndTime desc , (CASE WHEN ISNUMERIC(TagName)=1 THEN CAST(CAST(TagName AS float) AS INT)END ) desc"
这个怎么样 -
DELETE YOUR_TABLE
WHERE DATE_COL BETWEEN (SELECT MIN(DATE) FROM YOUR_TABLE) AND (SELECT MIN(DATE) FROM YOUR_TABLE) + 7
按照 Ankit 的建议,使用日期的 min() 并添加一周
delete
from dbo.statistic
where dateandtime between min(dateandtime)
and dateadd(week, 1, min(dateandtime))
MS SQL 2008R2 Express,我有数据库 - 一个 table 有行。当 db 大小变成大约 10gb 时,我需要在这个 table 中清理最早的一周(我不知道日期)。你能帮我写剧本吗?谢谢
UPD1。 有 DateAndTime 列,所以我在 VBA:
中进行这样的查询strSQL = "SELECT DateAndTime
,TagName
,Val
,SetPoint
,Limit_H
,Limit_L
,Result
FROM dbo.Statistic
WHERE DateAndTime BETWEEN CAST('" & TimeBegin & "' AS datetime) AND CAST( '" & TimeEnd & " ' AS datetime)
Order By DateAndTime desc , (CASE WHEN ISNUMERIC(TagName)=1 THEN CAST(CAST(TagName AS float) AS INT)END ) desc"
这个怎么样 -
DELETE YOUR_TABLE
WHERE DATE_COL BETWEEN (SELECT MIN(DATE) FROM YOUR_TABLE) AND (SELECT MIN(DATE) FROM YOUR_TABLE) + 7
按照 Ankit 的建议,使用日期的 min() 并添加一周
delete
from dbo.statistic
where dateandtime between min(dateandtime)
and dateadd(week, 1, min(dateandtime))