Quantcast
Channel: SCN : All Content - All Communities
Viewing all articles
Browse latest Browse all 3208

Execute Stored Procedure that uses "ALTER TABLE" and "DELETE FROM"

$
0
0

Business requirement: append the monthly new data into an archive table, then delete the oldest month data in the archive table. The archive table only keeps the most recent two months of data.


Example: we have February and March data in ARCHIVE. When April data comes, we append April data to the table, then delete February.


Our solution: create a stored procedure, steps below:


  1. Insert data into Archive table (sql: INSERT INTO)
  2. Add a column called “FLAG” (ALTER TABLE)
  3. Assign value to “FLAG”, assign 1 to the most recently two months, assign 0 to others (UPDATE)
  4. Delete records that have “FLAG” = 0 (DELETE FROM)
  5. Delete column “FLAG”(ALTER TABLE)
  6. Repeat every month

 

Question:

 

Since my procedure involves ALTER TABLE and DELETE FROM, I can’t execute them all at once because they depend on others. What’s the best way to run all these steps in sequence? Do I need 4 different procedures, call one by one, or is there a better way to execute all these procedures?


Note: I tried to use dynamic EXEC but I wasn't allow to exec ALTER TABLE using EXEC.


Viewing all articles
Browse latest Browse all 3208

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>