我有一张表,我每天都要输入数据。但我不希望其他用户修改此数据。所以我使用以下方法将这些数据备份到另一个表:
INSERT INTO tbl_cancel_backup
SELECT tbl_cancel.[cdate] AS 'cdate',
tbl_cancel.[machine_no] As 'No',
Sum(tbl_cancel.[amount]) AS 'Total'
FROM tbl_cancel
WHERE tbl_cancel.[Cdate]=@canceldate
GROUP BY tbl_cancel.[Machine_no], tbl_cancel.[cdate];
在此操作之后,我想删除2天前的记录。我如何修改下面的代码到正确的工作?
DELETE FROM tbl_cancel WHERE cdate = CONVERT (date, GETDATE()-2);
感谢大家。
答案 0 :(得分:1)
您想使用DateAdd
T-SQL
http://msdn.microsoft.com/en-us/library/ms186819.aspx
DELETE FROM tbl_cancel WHERE cdate <= DATEADD(d, -2, GETDATE())
答案 1 :(得分:1)
从tbl_cancel删除,其中cdate&lt; = dateadd(d,-2,getdate())
答案 2 :(得分:0)
您可以尝试使用DATEADD语法:
DELETE FROM tbl_cancel WHERE cdate <= CONVERT (date, DATEADD(dd,-2,GETDATE());