Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete old records in SQL database 3

Status
Not open for further replies.

ironhide1975

Programmer
Feb 25, 2003
451
US
I'm trying to setup a way for SQL server to automatically delete records that are older than 30 days. The current working script I have is.

CREATE PROCEDURE [dbo].[deleteOldRecords] AS

@CutOffDate = DATEADD(dd, -30, CURRENT_TIMESTAMP)

DELETE FROM TrackUser WHERE DateTime < @CutOffDate

Can anyone help me write this? Much appreciated if so.

Therm-o-disc -
 
CREATE PROCEDURE [dbo].[deleteOldRecords] AS

@CutOffDate = DATEADD(dd, -30, GetDate())

DELETE
FROM TrackUser
WHERE
CONVERT(Char,DateTime,110) < CONVERT(Char,@CutOffDate,110)


Thanks

J. Kusch
 
What's wrong with what you have?

Only question I have is 'what is CURRENT_TIMESTAMP'? I would use GETDATE() which is the system date/time.

-SQLBill
 
Just declare your variable before you try to set it
Code:
CREATE PROCEDURE [dbo].[deleteOldRecords] AS

DECLARE @CutOffDate smalldatetime
@CutOffDate = DATEADD(dd, -30, GetDate())

DELETE 
FROM TrackUser 
WHERE 
CONVERT(Char,DateTime,110) < CONVERT(Char,@CutOffDate,110)


Hope This Helps!

Ecobb

&quot;My work is a game, a very serious game.&quot; - M.C. Escher
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top