Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

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)

1NF View Generator

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!

Total article views: 1691 | Views in the last 30 days: 4
Related Articles

Script Out Foreign Keys With Multiple Keys

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?

Script to find Foreign Key missing Index?


Script foreign keys

Using this script you can create a list of sql commands for foreign keys manipulation.


Foreign Keys

Foreign keys are an important part of a relational database. New author Ravi Lobo takes a look at fo...


Update Script to select

Update Script to select


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones