Those of us who's roles require them to conduct demonstrations and presentations of SQL Server regularly will be familiar with the AdventureWorks databases. I must admit this has been my go to demo database for a while now. However some times the whole schema is just too much and all I really want is a subset of the schema for a demonstration.
The problem is because of the number of Constraints, Foreign Keys, and Views in the AdventureWorks schema this is no simple task (as it shouldn't be). I was faced with this issue today and so have created the following statements to help with generating the drop statements. These are based on the schema's that the objects belong to. For my needs I only wanted the HumanResources and Person schemas.
Important... backup first if you care about your data and..... DO NOT DO THIS IN PRODUCTION.....
Legal Stuff: As always the contents of this blog is provided “as-is”. The information, opinions and views expressed are those of the author and do not necessarily state or reflect those of any other company with affiliation to the products discussed. This includes any URLs or Tools. The author does not accept any responsibility from the use of the information or tools mentioned within this blog, and recommends adequate evaluation against your own requirements to measure suitability.
The problem is because of the number of Constraints, Foreign Keys, and Views in the AdventureWorks schema this is no simple task (as it shouldn't be). I was faced with this issue today and so have created the following statements to help with generating the drop statements. These are based on the schema's that the objects belong to. For my needs I only wanted the HumanResources and Person schemas.
Important... backup first if you care about your data and..... DO NOT DO THIS IN PRODUCTION.....
--Constraints SELECT sys.schemas.name as 'Schema' , sys.tables.name as 'Table' , sys.check_constraints.name as 'Constraint' , 'ALTER TABLE [' + sys.schemas.name + '].[' + sys.tables.name + '] DROP CONSTRAINT [' + sys.check_constraints.name + ']' AS 'Drop Stmt' FROM sys.check_constraints INNER JOIN sys.tables ON sys.tables.object_id = sys.check_constraints.parent_object_id INNER JOIN sys.schemas ON sys.schemas.schema_id = sys.tables.schema_id AND sys.schemas.name <> 'HumanResources' AND sys.schemas.name <> 'Person' --Foreign Keys SELECT sys.schemas.name as 'Schema' , sys.tables.name as 'Table' , sys.foreign_keys.name as 'ForeignKey' , 'ALTER TABLE [' + sys.schemas.name + '].[' + sys.tables.name + '] DROP CONSTRAINT [' + sys.foreign_keys.name + ']' AS 'Drop Stmt' FROM sys.foreign_keys INNER JOIN sys.tables ON sys.tables.object_id = sys.foreign_keys.parent_object_id INNER JOIN sys.schemas ON sys.schemas.schema_id = sys.tables.schema_id AND sys.schemas.name <> 'HumanResources' AND sys.schemas.name <> 'Person' --Views SELECT sys.schemas.name as 'Schema' , sys.views.name as 'View' , 'DROP VIEW [' + sys.schemas.name + '].[' + sys.views.name + ']' AS 'Drop Stmt' FROM sys.views INNER JOIN sys.schemas ON sys.schemas.schema_id = sys.views.schema_id AND sys.schemas.name <> 'HumanResources' AND sys.schemas.name <> 'Person' --Tables SELECT sys.schemas.name as 'Schema' , sys.tables.name as 'Table' , 'DROP TABLE ' + sys.schemas.name + '.' + sys.tables.name AS 'Drop Stmt' FROM sys.tables INNER JOIN sys.schemas ON sys.schemas.schema_id = sys.tables.schema_id AND sys.schemas.name <> 'HumanResources' AND sys.schemas.name <> 'Person' ORDER BY sys.schemas.name, sys.tables.name
Legal Stuff: As always the contents of this blog is provided “as-is”. The information, opinions and views expressed are those of the author and do not necessarily state or reflect those of any other company with affiliation to the products discussed. This includes any URLs or Tools. The author does not accept any responsibility from the use of the information or tools mentioned within this blog, and recommends adequate evaluation against your own requirements to measure suitability.
No comments:
Post a Comment