如何删除MS SQL数据库中最早的周(句点)?

时间:2017-09-26 10:20:51

标签: sql sql-server vba sql-server-2008r2-express

MS SQL 2008R2 Express,我有数据库 - 一个包含行的表。当db大小变成大约10gb时,我需要在这个表中清理一个最早的一周(我不知道日期)。你能用脚本帮我吗?感谢。

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"

2 个答案:

答案 0 :(得分:1)

这个怎么样 -

DELETE YOUR_TABLE
WHERE DATE_COL BETWEEN (SELECT MIN(DATE) FROM YOUR_TABLE) AND (SELECT MIN(DATE) FROM YOUR_TABLE) + 7

答案 1 :(得分:1)

正如Ankit建议的那样,使用日期的min()并添加一周

delete 
from dbo.statistic
where dateandtime between min(dateandtime) 
                    and dateadd(week, 1, min(dateandtime))