0

I got a task that i need to delete records from the ff tables below based on create dates.

Table Name: TableA Description: delete records that were created 7 days earlier.

Table Name: TableB Description: delete records that were created 7 days earlier.

Table Name: TableC Description: delete records that were created 6 days earlier.

Table Name: TableD Description: delete records that were created 6 days earlier.

Table Name: TableE Description: delete records that were created 6 days earlier.

Table Name: TableF Description: delete records that were created 6 days earlier.

Here is the code that i created

BEGIN TRY  
  
DELETE FROM TableA
 WHERE Createdttm < DATEADD(DAY,-7,GETDATE())    
  
DELETE FROM TableB
 WHERE Createdttm < DATEADD(DAY,-7,GETDATE())   
  
DELETE FROM TableC
 WHERE Createdttm < DATEADD(MONTH,-6,GETDATE())   
  
DELETE FROM TableD
 WHERE Createdttm < DATEADD(MONTH,-6,GETDATE())   
  
DELETE FROM TableE  
 WHERE Createdttm < DATEADD(MONTH,-6,GETDATE())   
  
DELETE FROM TableF 
 WHERE Createdttm < DATEADD(MONTH,-6,GETDATE())   
  
END TRY 

Can anyone help me create a loop script on this one? I will really appreciate your help. Thanks.

P.Salmon
  • 17,104
  • 2
  • 12
  • 19
  • Mysql does not have try,getdate or dateadd so I have changed the tag to sqlserver. – P.Salmon Jul 09 '20 at 08:26
  • Have a look at the second answer in the duplicate question, if you want to do it all in the database. – fancyPants Jul 09 '20 at 08:27
  • 1
    Sorry for closing as duplicate. The tag was changed afterwards. I'm voting to reopen the question. – fancyPants Jul 09 '20 at 08:28
  • @fancyPants how can i reopen again this question? Im sorry im a newbie here in stackoverflow. – James Heis Jul 09 '20 at 08:39
  • You can't do anything in this case. I already voted to reopen. The question is now in a review queue. One or two others will also have a look at it and reopen the question. Please have a little bit more patience. And again, sorry for the inconvenience. – fancyPants Jul 09 '20 at 08:48
  • No worries. Thanks @fancyPants. – James Heis Jul 09 '20 at 08:54
  • Any other way would result in using dynamic SQL, and IMO the standard way is better. – TomC Jul 09 '20 at 09:20
  • 1
    No idea what a "loop script" is nor what your actual goal is. Clarify that and the question might be reopened. But it should remain closed without more details. – SMor Jul 09 '20 at 11:51
  • Why do you want to loop? Your code says/does exactly what is needed. You could avoid repetition by using variables instead of repeating `dateadd()` expressions, but loops don't seem able to help. – underscore_d Jul 09 '20 at 12:05

0 Answers0