SELECT b.rkey as ColumnsId,
(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid) as ColumnsName,
b.rkeyid as TabaleId,OBJECT_NAME(b.rkeyid) as TableName
,b.fkey as ForeignKeyId
,(SELECT name FROM syscolumns WHERE colid=b.fkey AND id=b.fkeyid) as ForeignKeyName,
b.fkeyid as ForeignKeyTableId
,object_name(b.fkeyid) AS ForeignKeyTableName
,ObjectProperty(a.id,'CnstIsUpdateCascade') as CnstIsUpdateCascade
,ObjectProperty(a.id,'CnstIsDeleteCascade') as CnstIsDeleteCascade
FROM sysobjects a
join sysforeignkeys b on a.id=b.constid
join sysobjects c on a.parent_obj=c.id
where a.xtype='f' AND c.xtype='U'
and b.rkeyid in (select id from sysobjects as d where d.xtype = 'U' AND d.status >= 0 )

select distinct t.id as TableId ,t.name as TableName,rtrim(t.xtype) as TableType ,o.object_id as RelatedId,rtrim(o.name) as RelatedName,o.type as RelatedType,o.type_desc as RelatedTypeDesc,o.create_date as CreateDate,o.modify_date as ModifyDate
from sys.sql_dependencies as s inner join sys.objects as o on s.object_id=o.object_id
inner join (select id,name,d.xtype from sysobjects as d where d.status >= 0) as t on t.id=referenced_major_id
where t.name<>'sysdiagrams'
获取视图名称：
select newid() as Id,'' as Remark,'' as ObjectType,
Row_Number() over ( order by getdate() ) as SortId,v.object_id as ObjectId,v.name as ObjectName ,m.definition as Contents
from sys.views v left outer join sys.all_sql_modules as m on v.object_id=m.object_id
order by ObjectId

获取视图列：

Select newid() as Id,c.id as ColumnsId,o.id as ColumnsObjectId,o.Name As ColumnsObjectName,c.name As ColumnsName, t.name As ColumnsType, c.length As ColumnsLength,
ISNULL(COLUMNPROPERTY(c.id, c.name, 'Scale'), 0) AS DecimaPlaces,
CASE WHEN COLUMNPROPERTY(c.id,c.name, 'IsIdentity') = 1 THEN '是' ELSE '' END AS Mark,
CASE WHEN c.isnullable = 0 THEN '√' ELSE '' END AS Primarykey,
CASE WHEN c.isnullable = 1 THEN '√' ELSE '' END AS IsEmpty,
'' AS Defaults,'' AS ColumnsRemark,
c.colorder as ColumnsSortId
From SysObjects As o , SysColumns As c , SysTypes As t
Where o.type ='v' And o.id = c.id And c.xtype = t.xtype and t.name<>'sysname'
Order By o.name ,c.colorder

获取函数/存储过程：

select O.object_id as FunctionId, O.name as FunctionName,rtrim(O.type) as FunctionType,O.create_date as Createtime, definition as Contents,'' as FunctionReamrk
from sys.objects O LEFT OUTER JOIN sys.extended_properties E ON O.object_id = E.major_id
left outer join sys.all_sql_modules on all_sql_modules.object_id=O.object_id
WHERE O.name IS NOT NULL AND ISNULL(O.is_ms_shipped, 0) = 0 AND ISNULL(E.name, '') <> 'microsoft_database_tools_support' AND O.type in ('FN', 'IF', 'TF','P')
ORDER BY O.name

获取函数/存储过程的参数：

SELECT sp.object_Id as FunctionId, sp.name as FunctionName,
isnull(param.name,'')as ParamName,isnull(usrt.name,'') AS [DataType],
ISNULL(baset.name, '') AS [SystemType], CAST(CASE when baset.name is null then 0 WHEN baset.name IN ('nchar', 'nvarchar') AND param.max_length <> -1 THEN param.max_length/2 ELSE param.max_length END AS int) AS [Length],
'' as ParamReamrk,isnull(parameter_id,0) as SortId
FROM sys.objects AS sp INNER JOIN sys.schemas b ON sp.schema_id = b.schema_id
left outer JOIN sys.all_parameters AS param ON param.object_id=sp.object_Id
LEFT OUTER JOIN sys.types AS usrt ON usrt.user_type_id = param.user_type_id
LEFT OUTER JOIN sys.types AS baset ON (baset.user_type_id = param.system_type_id and baset.user_type_id = baset.system_type_id) or ((baset.system_type_id = param.system_type_id) and (baset.user_type_id = param.user_type_id) and (baset.is_user_defined = 0) and (baset.is_assembly_type = 1))
LEFT OUTER JOIN sys.extended_properties E ON sp.object_id = E.major_id
WHERE sp.TYPE in ('FN', 'IF', 'TF','P') AND ISNULL(sp.is_ms_shipped, 0) = 0 AND ISNULL(E.name, '') <> 'microsoft_database_tools_support'
ORDER BY sp.name,param.parameter_id ASC

获取触发器：

select t.object_id as TriggersId, t.name as TriggersName,t.create_date as Createtime,t.type as FunctionType,
parent_id as TableId,sp.name as TableName,definition as Contents,'' as TriggersRemark from sys.triggers as t inner join sys.all_sql_modules as m
on t.object_id=m.object_id
inner join sys.objects as sp on t.parent_id=sp.object_id