Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Deleting data automatically-urgent

Author  Topic 

kvt.aravind
Starting Member

24 Posts

Posted - 2010-08-24 : 02:46:22
To delete the records older than 90 days once in a month automatically... How to do it Kindly help


delete from [TATA_TRAVEL_EXPENSEINFONEW] WHERE [TATA_TRAVEL_EXPENSEINFONEW].EXP_DATE < dateadd(dd,-90,getdate()) AND [TATA_TRAVEL_EXPENSEINFONEW].STATUS IN ('Open')
go

delete from [TATA_TRAVEL_EXPENSEINFONEW] WHERE [TATA_TRAVEL_EXPENSEINFONEW].EXP_DATE < dateadd(dd,-90,getdate()) AND [TATA_TRAVEL_EXPENSEINFONEW].STATUS IN ('DELETED')
go


delete from tata_travel_ticketinfo where status in ('Deleted')
go

Delete from tata_travel_ticketinfo Where tata_travel_ticketinfo.book_id in (select tata_travel_ticketinfo.book_id from tata_travel_ticketinfo inner join tata_travel_bookticketnew on tata_travel_bookticketnew.book_id = tata_travel_ticketinfo.book_id and tata_travel_ticketinfo.Status in ('open') and tata_travel_ticketinfo.created_on < dateadd(dd,-90,getdate()))
go


normally i execute the above query once in a month to delete the records in open status and deleted status from two tables but i need this to be done automaticaly once in a month...kindly suggest...its urgent..

Regards
Aravind.T

Aravind.T

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-24 : 03:32:56
Schedule it to run via a SQL Agent job.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

kvt.aravind
Starting Member

24 Posts

Posted - 2010-08-24 : 04:38:31
can we schedule the above queries in single stored procedure or query by query

Aravind.T
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-08-24 : 05:00:51
quote:
Originally posted by kvt.aravind

can we schedule the above queries in single stored procedure or query by query

Aravind.T


It is better to have them in a procedure

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

kvt.aravind
Starting Member

24 Posts

Posted - 2010-08-24 : 05:25:23
Is It possible to store the records in separate table before they are deleted. I used select * into XXX inside the stored procedure to get the records which are beyond 90 days but i excuted first timeie., first month it executed successfully but second time the next month if i execute it displays invalid object name xxx

Aravind.T
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-24 : 06:29:52
You would need to use INSERT INTO/SELECT and not SELECT * INTO. But why do you need to store them in a separate table? If you need to this, you should consider partitioning the table.

Your error would have been duplicate object and not invalid object.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

kvt.aravind
Starting Member

24 Posts

Posted - 2010-08-24 : 06:45:36
Incase the client needs to see the deleted records means...I can get the data from these tables...Partition of table means?

Aravind.T
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-24 : 06:46:56
If you partition the table, then you can easily archive the partitions. You can partition by date to achieve your results.

Look up partitioning in Books Online and google it for more information.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -