Skip to content

Instantly share code, notes, and snippets.

Last active April 28, 2021 22:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
WITH tables AS
SELECT AS schema_name
, AS table_name
, CONCAT(, '.', AS full_name
FROM sys.tables
JOIN sys.schemas
ON tables.schema_id = schemas.schema_id
WHERE tables.type = 'U'
, CONCAT('DELETE FROM ', full_name) AS delete_script
, CONCAT('TRUNCATE TABLE ', full_name) AS truncate_script
, CONCAT('DROP TABLE IF EXISTS ', full_name) AS drop_script
, CONCAT('UPDATE STATISTICS ', full_name) AS update_script
, CONCAT('ALTER TABLE ', full_name, ' NOCHECK CONSTRAINT ALL') AS disable_constraints_script
, CONCAT('ALTER TABLE ', full_name, ' WITH CHECK CHECK CONSTRAINT ALL') AS enable_constraints_script
, CONCAT('IF NOT EXISTS(SELECT TOP 1 1 FROM ', full_name, ') THROW 51000, TABLE ''', full_name, ''' has zero rows.'', 1;') AS require_data_script
FROM tables
ORDER BY full_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment