Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.20 KB | None | 0 0
  1. CREATE PROCEDURE sw_Purge @counter INT = 1
  2. AS
  3. BEGIN
  4.       SET NOCOUNT ON;
  5.      
  6.  /*     COMMENT
  7.  
  8.         This is the method to create the table to work from
  9.  
  10.         SELECT  subscriber_id,
  11.         CAST(0 AS INT) AS Purged
  12.         INTO PurgedSubs
  13.         FROM subscribers s
  14.         where
  15.             s.act_date is null and
  16.             s.state = 4                    
  17.                                                 END COMMENT */
  18.            
  19. DECLARE @subID INT
  20. --DECLARE @counter INT = 1
  21.  
  22. DECLARE purgeCursor CURSOR FOR  
  23. SELECT top (SELECT @counter) subscriber_id FROM purgedSubs p WHERE p.purged =0
  24.  
  25. OPEN purgeCursor  
  26.  
  27. FETCH NEXT FROM purgeCursor INTO @subID
  28.  
  29.  
  30. WHILE @@FETCH_STATUS = 0  
  31. BEGIN  
  32.    
  33.     BEGIN
  34.  
  35.     -- Delete all notes
  36.     DELETE FROM subscriber_notes WHERE subscriber_id = @subID
  37.  
  38.     --Delete all tickets
  39.     DELETE FROM tickets WHERE reference_id = @subID
  40.        
  41.     --Create an Audit Note
  42.     INSERT INTO subscriber_notes
  43.     (creator_id,date_created,date_last_updated,editor_id,note,note_type,subscriber_id) VALUES
  44.     ( 1,GETDATE(),NULL,NULL,'UK-90807 : User ticket and note data has been purged',0,@subid)
  45.        
  46.     UPDATE purgedSubs SET purged = 1 WHERE subscriber_id = @subid
  47.     END
  48.    
  49.  
  50. FETCH NEXT FROM purgeCursor INTO @subID  
  51. END  
  52.  
  53. CLOSE purgeCursor
  54. DEALLOCATE purgeCursor
  55. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement