Need to drop a list of tables and should be made configurable in SQL SERVER -


as part of new release, take backups of few tables , delete on stabilisation period. want make configurable, have naming convention set list of tables want delete , drop after stabilisation period over

here plan.

  1. rename tables want delete, suffix "_tbr" (to removed).
  2. add suffix yyyymmdd format, eg. if name of table employeedetails, employeedetails_tbr_20150614.

thus, create stored procedure, select tables has suffix of tbr , place temp table, along date time eg

select      name 'tablename',     convert (datetime, substring (name, len(name)-7,8)) 'deletedon'      sys.tables       name '%_tbr_%' ,      type ='u' 

now

select tablename #temptable deletedon <=getdate() 

this select tables beyond specified date. now, declare cursor, , loop in , delete it.

i schedule job run stored procedure every week, , take care of.

is there other smarter , optimized way of doing it?

one method:

create proc dbo.dropbackuptables  declare @dropscript nvarchar(max) = (     select n'drop table ' + quotename(object_schema_name(object_id)) + n'.' + quotename(name) + n';'     sys.tables             name n'%[_]tbr[_][2][0-9][0-9][0-9][0-1][0-9][0-3][0-9]'         , right(name, 8) <= convert(char(8), getdate(), 112)         xml path('')         );  exec sp_executesql @dropscript; go 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -