Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin.
Search for scripts directly from SSMS, and instantly access any saved scripts in your
SSC briefcase from the favorites tab.
Download now (direct download link)
Thank this author by sharing:
By Mark Stockwell,
Often times a DBA or BI developer needs to quickly create a view or select statement against a cryptic transactional table as part of an ETL or reporting requirement. The typical process involves selecting all columns from the base table, looking for foreign keys, finding the referenced tables, figuring out if outer join needed, adding tables to the FROM clause, etc. This is often a time consuming task, especially if foreign key columns are named differently from base table foreign key columns.
The attached script jump starts the process. SSMS has a nice context menu option to "Select top 1000 rows", this script does similar query, except it links to all the foreign key tables and retreives all the columns from them. It also adds joins, correctly adding LEFT OUTER when needed. The primary keys are indicated by adding a "_PK" to the column alias.
To use just set the @tab variable at the beginning of the script. The output is an XML document with View DDL. The view is essentially a denormalized version of the base table. This view can be used for basic reporting, ETL extraction, or creating a history table for volatile base tables. Hope you find it useful!
It's easy enough to use sys.foreign_keys and sys.foreign_key_columns to identify foreign keys. But ...
Script to find Foreign Key missing Index?
Using this script you can create a list of sql commands for foreign keys manipulation.
Foreign keys are an important part of a relational database. New author Ravi Lobo takes a look at fo...
Update Script to select