首页 > 代码库 > MSSQL中数据库对象类型解释

MSSQL中数据库对象类型解释

public string GetObjectTypeName(object oType){switch (oType+""){case "U":return "用户表";case "V":return "视图";case "P":return "存储过程";case "TF":return "表函数";case "FN":return "标量函数";case "C":return "CHECK约束";case "D":return "默认值或DEFAULT约束 ";case "F":return "FOREIGN KEY 约束 ";case "IF":return "内嵌表函数 ";case "K":return "PRIMARY KEY或 UNIQUE约束";case "L":return "日志";case "R":return "规则";case "RF":return "复制筛选存储过程";case "S":return "系统表";case "TR":return "触发器";case "X":return "扩展存储过程";}return "";}
查询数据库脚本:SELECT name AS DbName,[dbid] As DbId,filename As FileName FROM [Master].dbo.[sysdatabases] Where dbid>4 Order by name查询表脚本:SELECT [{0}].dbo.sysobjects.name As TableName,id As TableId,(Select top 1 rowcnt From [{0}].dbo.sysindexes Where id=[{0}].dbo.sysobjects.id) As RecordCount,ISNULL(E.name,‘‘) As PropertyName,ISNULL(E.value,‘‘) As PropertyValueFROM [{0}].dbo.sysobjectsLEFT JOIN sys.extended_properties E ON E.major_id=[{0}].dbo.sysobjects.id AND E.minor_id=0Where xtype=U AND [{0}].dbo.sysobjects.name not in (dtproperties,sysdiagrams) Order By [{0}].dbo.sysobjects.name查询数据库对象脚本:SELECT T.name As ObjectName,T.id As ObjectId,rTRIM(T.xtype) AS ObjectType,(SELECT Top 1 A.[definition] FROM [{0}].sys.sql_modules A JOIN [{0}].sys.objects B ON A.object_id = B.object_idWhere B.name=T.name) As ObjectDefinitionFROM [{0}].dbo.sysobjects TWhere xtype in ( NP, NPC,NFN, NIF, NTF, NFS, NFT, NV,NTR)AND T.name not in (dtproperties,sysdiagrams)Order By T.name查询列与字段:SelectC.name As TableName,C.id As TableObjectId,A.name As ColumnName,A.colid As ColumnObjectId,A.[length] As ColumnLength,B.name As ColumnDataType,ISNULL(D.[text],‘‘) AS DefaultValue,ISNULL(E.name,‘‘) As PropertyName,ISNULL(E.value,‘‘) As PropertyValue,COLUMNPROPERTY(A.id,A.name, AllowsNULL) AS AllowsNull,COLUMNPROPERTY(A.id,A.name, IsCursorType) AS IsCursorType,COLUMNPROPERTY(A.id,A.name, IsIdentity) AS IsIdentity,(Select Count(1) FromsysobjectsINNER JOIN sysindexes ON sysobjects.parent_obj=sysindexes.id AND sysobjects.name=sysindexes.nameINNER JOIN sysindexkeys ON sysindexes.id=sysindexkeys.id AND sysindexes.indid=sysindexkeys.indidWhere sysobjects.xtype=PK AND sysobjects.parent_obj=A.id AND sysindexkeys.colid=A.colid) AS IsPrimaryKeyFromdbo.syscolumns AINNER JOIN dbo.systypes B ON B.xusertype=A.xtypeINNER JOIN dbo.sysobjects C ON C.id=A.id AND C.xtype IN (U,V) AND C.name not in (dtproperties,sysdiagrams)LEFT JOIN dbo.syscomments D ON A.cdefault = D.idLEFT JOIN sys.extended_properties E ON E.major_id=A.id AND E.minor_id=A.colid order by A.colid查询约束脚本:SELECTb.rkeyid AS MasterTableObjectId,object_name(b.rkeyid) AS MasterTableName,b.rkey As MasterColumnObjectId,(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid) AS MasterColumnName,b.fkeyid AS ForeignTableObjectId,object_name(b.fkeyid) AS ForeignTableName,b.fkey AS ForeignColumnObjectId,(SELECT name FROM syscolumns WHERE colid=b.fkey AND id=b.fkeyid) As ForeignColumnName,ObjectProperty(a.id,CnstIsUpdateCascade) AS UpdateCascade,ObjectProperty(a.id,CnstIsDeleteCascade) AS DeleteCascadeFROM sysobjects ajoin sysforeignkeys b on a.id=b.constidjoin sysobjects c on a.parent_obj=c.idWHERE a.xtype=F AND c.xtype=U

 

参考:

 http://blog.ueren.com/?tag=sysobjects

MSSQL中数据库对象类型解释