List tables, columns and defaults in a database

The code snippet lists all tables, columns, data type, null-ability and default values for a database. Tested on MS SQL Server 2005. Handy for a quick check of data types and defaults.

/* List tables, columns, data type and default values */
USE ASI2353;
SELECT   [TABLE_NAME] AS "Table"
		,[COLUMN_NAME] AS "Column"
		,[DATA_TYPE] AS "Data Type"
		,[CHARACTER_MAXIMUM_LENGTH] AS "Data Length"
		,[COLUMN_DEFAULT] AS "Default"
		,[IS_NULLABLE] AS "Nullable"
	FROM information_schema.columns
WHERE [TABLE_NAME] != 'sysdiagrams'
ORDER BY [TABLE_NAME];

The last table

Well, not really. Within a Data Pump SQL project it is called [dbo].[Last] and is primarily intended to be queried by third-party applications, like SCADA, EP, CEP or reporting tools. The table contains only one row with the latest serial number and the time-stamp from the [dbo].[PartData] table. Here is a typical example:

ID Val TimeUTC TimeLoc
1 200805151007 2008-05-15 14:30:01 2008-05-15 10:30:01

 

The table structure may vary, depending on the project. Time column in UTC allows for applications outside of the time zone.

Column Name Data Type Description
ID int ID column for this table; always 1 (one row only).
Val bigint Serial number of the last entry from [dbo].[PartData].
TimeUTC datetime Time-stamp of the last entry in UTC (GMT).
TimeLoc datetime Optional. Local time of the last entry.

 

The “third-party” application should query the [dbo].[Last] table and determine if there were any changes to the [dbo].[PartData] table since the last query.

SELECT [Val], [TimeLoc] 
    FROM [dbo].[Last] WHERE ID = 1;

Once a change is detected, it is OK to query the [dbo].[PartData] table.

    
/* Time of the last query */
DECLARE @LastTime datetime;
SET @LastTime = '2008-05-15 14:30:01';
--
/* Query the table */
SELECT * 
    FROM [dbo].[PartData]
WHERE [RecordDate]  >= @LastTime;

Standard tables

Here is the list of tables in a typical Data Pump SQL project:

Object Name Schema Type Description
ClientPrivate dbo TABLE Client (Excel) setup info
Error dbo TABLE Error codes & descriptions
Field dbo TABLE Fields collected in the dbo.PartData
Header dbo TABLE Collection of header fields and order in reports
Limit dbo TABLE Process limits; LSL, USL
PartData dbo TABLE One row for each part
Station dbo TABLE Station names and IDs
Style dbo TABLE Style names and IDs
Last dbo TABLE Serial number and time-stamp of the last entry to the PartData table

 

For more details see the knowledge database article.