Solved

SQL Server2005

Posted on 2008-06-09
2
182 Views
Last Modified: 2010-03-19
I need to implement a  delete proc to delete all records from a table which are older than 60days.
I just wrote a proc and compared with a field (dtInsert) from the table.....I feel I went wrong somewhere....I need help to correct this proc.....
ALTER PROCEDURE [dbo].[pr_Expire]
 

AS

BEGIN

    SET NOCOUNT ON;

    

    --Calculate expiration date   

    DECLARE @expire DATETIME

    SEt @expire = GETDATE()

    SET @expire = DATEADD(day,-60,@expire)
 
 

    --Expire all records older than @expire

    DELETE FROM dbo.CP_EquiP

     WHERE dtInsert < @expire

END

Open in new window

0
Comment
Question by:onebite2
  • 2
2 Comments
 
LVL 13

Expert Comment

by:Wizilling
ID: 21748051
The procedure seems correct. why do u think u went wrong somewhere..
or is it that u had to include the -60th day also .. in that case u need to change your where clause to
WHERE dtInsert <= @expire
0
 
LVL 13

Accepted Solution

by:
Wizilling earned 500 total points
ID: 21748118
In fact to include the 60th day as well you will need to calculate the 59th day and convert it to 000 hours ie mid night and query everythnig less then it.
ALTER PROCEDURE [dbo].[pr_Expire]

 

AS

BEGIN

    SET NOCOUNT ON;

    

    --Calculate expiration date   

    DECLARE @expire DATETIME

    SEt @expire = CONVERT(varchar(25), GETDATE(),112)

    SET @expire = DATEADD(day,-59,@expire)

  

    --Expire all records older than @expire

    DELETE FROM dbo.CP_EquiP

     WHERE dtInsert < @expire

END

Open in new window

0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now