posts - 217, comments - 218, trackbacks - 239

My Links

News




I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net) and born in Bangladesh.
I work for Ocean Informatics Pty Ltd as a Senior Developer - Analyst.
I am also co-founder and core developer of Pageflakes (acquired by LiveUniverse) www.pageflakes.com
and most recently created SmartCodeGenerator

My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

MS SQL 2000 Schema Discovery Queries

Below is the list of queries that I am upto with my MSSql2000DBSchemaProvider for SmartCodeGenerator. I thought this might be useful and quick reference for others too.

GetTables

string cmdText = string.Format(@"Select TABLE_NAME, TABLE_SCHEMA OWNER, REFDATE CREATE_TIME
FROM INFORMATION_SCHEMA.TABLES , sysobjects
where Table_Name = sysobjects.[name]
and TABLE_CATALOG = '{0}' AND TABLE_TYPE = 'BASE TABLE' ORDER BY 1", database.Name);

GetTableColumns

string commandText = string.Format(@"Select COLUMN_NAME, DATA_TYPE, IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, CHARACTER_SET_NAME, COLLATION_NAME, TABLE_NAME, COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), COLUMN_NAME, 'IsIdentity') as IS_IDENTITY
from INFORMATION_SCHEMA.COLUMNS
where Table_Name = '{0}'
and Table_Schema ='{1}'
order by ORDINAL_POSITION",table.Name, table.Owner);

GetTableKeys // ForeignKeys

string commandText = string.Format(@"SELECT t1.CONSTRAINT_NAME, t1.COLUMN_NAME, t1.ORDINAL_POSITION
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE t1, INFORMATION_SCHEMA.TABLE_CONSTRAINTS t2
Where t2.TABLE_CATALOG = t1.TABLE_CATALOG
AND t2.TABLE_SCHEMA = t1.TABLE_SCHEMA
AND t2.TABLE_NAME = t1.TABLE_NAME
AND t2.CONSTRAINT_NAME = t1.CONSTRAINT_NAME
and t1.TABLE_Catalog = '{0}' AND t1.TABLE_NAME = '{1}'
AND CONSTRAINT_TYPE = 'FOREIGN KEY'
order by ORDINAL_POSITION", table.Database.Name, table.Name);

Get Referential Info of a foreignkey_Constraint

string commandText1 = string.Format(@"select t1.CONSTRAINT_NAME, t1.COLUMN_NAME, t1.ORDINAL_POSITION, t1.TABLE_NAME
from Information_Schema.Key_Column_Usage t1, Information_Schema.Referential_constraints t2
where t2.Constraint_name = '{0}'
and t2.Unique_Constraint_Name = t1.Constraint_Name
and table_Catalog = '{1}'
order by Ordinal_Position", fk.ConstraintName, table.Database.Name);

Get PrimaryKey Schema

string commandText = string.Format(@"SELECT t1.CONSTRAINT_NAME, t1.COLUMN_NAME, t1.ORDINAL_POSITION
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE t1, INFORMATION_SCHEMA.TABLE_CONSTRAINTS t2
Where t2.TABLE_CATALOG = t1.TABLE_CATALOG
AND t2.TABLE_SCHEMA = t1.TABLE_SCHEMA
AND t2.TABLE_NAME = t1.TABLE_NAME
AND t2.CONSTRAINT_NAME = t1.CONSTRAINT_NAME
and t1.TABLE_Catalog = '{0}' AND t1.TABLE_NAME = '{1}'
AND CONSTRAINT_TYPE = 'PRIMARY KEY'
order by ORDINAL_POSITION", table.Database.Name, table.Name);

GetViews

string cmdText = string.Format(@"Select TABLE_NAME, TABLE_SCHEMA OWNER, REFDATE CREATE_TIME
FROM INFORMATION_SCHEMA.VIEWS , sysobjects
where Table_Name = sysobjects.[name]
and TABLE_CATALOG = '{0}'
and substring(VIEW_DEFINITION, 1, 1)!= 'C'
ORDER BY 1", database.Name);


GetViewText

string cmdText = string.Format(@"select VIEW_DEFINITION from INFORMATION_SCHEMA.VIEWS
where Table_Name = '{0}'
and Table_catalog ='{1}'
and Table_Schema ='{2}'", view.Name, view.Database.Name, view.Owner);

 

GetViewColumns

string commandText = string.Format(@"Select COLUMN_NAME, DATA_TYPE, IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, CHARACTER_SET_NAME, COLLATION_NAME, TABLE_NAME, COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), COLUMN_NAME, 'IsIdentity') as IS_IDENTITY
from INFORMATION_SCHEMA.COLUMNS
where Table_Name = '{0}'
and Table_Schema ='{1}'
order by ORDINAL_POSITION", view.Name, view.Owner);

Print | posted on Wednesday, January 03, 2007 5:55 AM |

Feedback

Gravatar

# re: MS SQL 2000 Schema Discovery Queries

Hi Shahed

Thanks for sharing this info! It was very helpful.

Regards
Michael
9/10/2008 6:09 AM | Michael Bürgi

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 8 and type the answer here:

Powered by: