[代码]SQLServer2005读取表信息

declare @schemaName varchar(100)
declare @tblName varchar(100)
declare @colName varchar(100)
declare @objectID int
set @schemaName = 'College'
set @tblName = 'Student'
set @colName = 'iStatusID'
set @objectID = OBJECT_ID(@schemaName + '.' + @tblName)
-- 1. How to check the Column is nullable?
Select COLUMNPROPERTY(@objectID,@colName,'AllowsNull') AS 'Allows Null?';
-- 2. How to check the Column is an identity?
Select COLUMNPROPERTY(@objectID,@colName,'IsIdentity') AS 'Identity?';
-- 3. How to check the Column is an FullTextEnabled?
Select COLUMNPROPERTY(@objectID,@colName,'IsFulltextIndexed') AS 'FullTextEnabled?';
-- 4. How to check the Column's datatype?
select b.name as 'Datatype', a.max_length, a.precision, a.scale
from sys.columns a
join sys.types b on a.system_type_id = b.system_type_id and a.user_type_id = b.user_type_id
where OBJECT_NAME(a.object_id) = @tblName and a.name = @colName
-- 5. How to identify that the default constraints have been created for the Column?
select a.name as 'Default Constraint Name'
from sys.default_constraints a
join sys.all_columns b on a.parent_object_id = b.object_id and a.parent_column_id = b.column_id
where object_name(parent_object_id) = @tblName and b.name = @colName
-- 6. How to identify that the foreignkey constraints for the table?
select name as 'ForeignKeyConstraint Name' from sys.objects
where type = 'F' and parent_object_id = @objectID
-- 7. Select all Constraints associated with this table and Column.
Select Constraint_Name from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
Where Table_Name = @tblName and Column_Name = @colName
-- 8. Find all the Objects from sys.objects table by specifying the "type".
select name from sys.objects Where parent_object_id = @objectID and type = 'D'