Retrieving Table-field Information Using T-sql

While creating a scalable CRM platform, you can reduce development efforts by automating data type and field definition in databound controls.

While creating a scalable CRM platform, you can reduce development efforts by automating data type and field definition in datagrids. The idea is to read the fields’ names, data types and default values and render the databound controls only by mentioning table names.

In order to obtain this information we will call this T-SQL code and get the field list for any given table in the database:

SELECT
syscolumns.Name as FieldName,
systypes.Name as TypeName,
syscomments.[Text] as DefaultValue
FROM syscolumns
LEFT JOIN systypes ON systypes.xtype=syscolumns.xtype AND systypes.xtype=systypes.xusertype
LEFT JOIN syscomments ON syscomments.id=syscolumns.cdefault
WHERE syscolumns.id = object_id(’TableName‘)

Good Luck!

Leave Your Response