Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old January 5th, 2005, 01:51 PM
Shelly Shelly is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 38 Shelly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 2 sec
Reputation Power: 5
Find date difference and delete record

I want to delete the records who older than 90 days. This code is not
working.Can anyone help please?


create PROCEDURE dbo.deleteReservation
@resID int,
@d int,
@diff int
as
set nocount on
Declare CurRes CURSOR for
SELECT DATEDIFF(d,reservationdate, getdate()),id from reservations
OPEN CurRes
FETCH NEXT FROM CurRes
INTO @diff,@resID
WHILE @@FETCH_STATUS = 0
BEGIN
IF @diff > 1
BEGIN
EXEC('DELETE FROM reservations WHERE id =' + @resID)
End
FETCH NEXT FROM CurRes
INTO @diff,@resID
END
CLOSE CurRes
DEALLOCATE CurRes
go

Reply With Quote
  #2  
Old January 5th, 2005, 03:12 PM
A2k's Avatar
A2k A2k is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 166 A2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 44 m 10 sec
Reputation Power: 4
hmmm... that seems a very complicated way of doing things...

here is my suggestion

Code:

create procedure dbo.deleteDates as

delete from
yourTable
where
dateDiff(d, oldDate, newDate)>90


hope this helps

BTW what is the result of your code? does it delete everything? or does it throw an error message.

Reply With Quote
  #3  
Old January 5th, 2005, 05:16 PM
Shelly Shelly is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 38 Shelly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 2 sec
Reputation Power: 5
Thanks for the suggestion. Now my code is working
properly.

Here is the code.

CREATE PROCEDURE dbo.dt_UpdateReservation_resStatus
AS
Declare @s_resID integer
Declare @s_diff integer
Declare CurRes CURSOR for
SELECT DATEDIFF(d,createdon,getdate()),id from reservations where is_reservation=0
OPEN CurRes
FETCH NEXT FROM CurRes
INTO @s_diff,@s_resID
WHILE @@FETCH_STATUS = 0
BEGIN
IF @s_diff = 1
BEGIN
EXEC('Update reservations set reservationstatus=8 where id =' + @s_resID)
End
FETCH NEXT FROM CurRes
INTO @s_diff,@s_resID
END
CLOSE CurRes
DEALLOCATE CurRes
GO

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Find date difference and delete record


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway