31/05/2015 R Tyler 0 Comments

After recently completing a database development project I found the actual database tables in use had gotten out of step with my original (planned) Entity Relationship Model and its associated Data Schema. The reason for the differences in my data tables (from those which I had planned for) was due to the fact that when carrying out normalisation of the tables (in situ) in the database, duplicated tables (some containing additional fields) were included in order to accommodate additional information which had been stored in the original (Excel) data source. To check that all the tables in the database matched the original model (and also to target additional unused rows – for later removal) I used the following script. By executing the script in a query window in Microsoft SQL Server Management Studio (R2) a resulting Table and Column name listing was produced. This script was referenced on the ‘sqlauthority’ website listed below. However I made a slight modification (further sources are cited) in order to remove ‘auto created tables’ from the list in order to leave only tables that I had actually added to the database structure myself in the list. This allowed me to focus my attention on the tables which I had inserted from the outset (as distinct from system auto generated tables). I hope you find the following scripts useful. Whilst carrying out research for this work I stumbled on this excellent script source on the Simple Talk website:

https://www.simple-talk.com/sql/database-administration/exploring-your-sql-server-databases-with-t-sql/

This source provides additional scripts for delving into all manner of database and table statistic in T-SQL. Its a post I will no doubt be returning to in the future.

The following script produces a listing of all Tables and Table Objects within a T-SQL database, including: TableName, ColumnName, SchemaName, TypeName, StatName, column_id and LastUpdated date. The sample shown is listing is for the familiar ‘Northwind’ database provided by Microsoft. Of course you’ll need to modify with your database name.

References:

http://blog.sqlauthority.com/2011/06/29/sql-server-find-details-for-statistics-of-whole-database-dmv-t-sql-script/

http://stackoverflow.com/questions/2418527/sql-server-query-to-get-the-list-of-columns-in-a-table-along-with-data-types-no

Leave a Reply: