Does anyone have or know of stored procedure that will do generic temporal table manipulation so that history and main table are both truncated?
You have to alter it to set system_versioning = off, then drop the history table, then truncate your main table, then re-enable versioning on your main table. I was going to build a generic stored procedure to do this for any database/schema/table name passed to it but sure be nice if someone already had one. I never like re-inventing the wheel if I can avoid it.
Best Answer
MSSQL documentation has a good sample stored procedure (though far from perfect, as Martin noted in the comments about how it does not use
QUOTENAME
, for one) for cleaning a temporal table:Just add a few extra lines to handle removing data from the parent table and you should be in business.
Bonus: it uses sysname for variables which I am a huge proponent of but is generally an overlooked datatype!