Skip to content

Instantly share code, notes, and snippets.

@dhmacher
Last active July 26, 2022 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dhmacher/875233700ef4743a97adb8b9f977ff80 to your computer and use it in GitHub Desktop.
Save dhmacher/875233700ef4743a97adb8b9f977ff80 to your computer and use it in GitHub Desktop.
Move temp/backup objects to Deprecated schema
IF (SCHEMA_ID('Deprecated') IS NULL)
EXEC('CREATE SCHEMA [Deprecated];');
SELECT 'ALTER SCHEMA [Deprecated] TRANSFER '+QUOTENAME(OBJECT_SCHEMA_NAME([object_id]))+'.'+QUOTENAME(OBJECT_NAME([object_id]))+';'
FROM sys.objects
WHERE [schema_id] NOT IN (SCHEMA_ID('Deprecated'), SCHEMA_ID('sys'))
AND [type] NOT IN ('PK', 'F', 'D', 'UQ')
AND ([name] LIKE '%xx%' OR [name] LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9]%'
OR [name] LIKE '%[ _]temp' OR [name] LIKE 'temp[ _]%'
OR [name] LIKE '%[ _]old' OR [name] LIKE 'old[ _]%'
OR [name] LIKE '%backup' OR [name] LIKE 'backup%')
ORDER BY OBJECT_SCHEMA_NAME([object_id]), OBJECT_NAME([object_id]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment