【转】MSSQL中数据库对象类型解释--觉得很好,自己存档

简介: 出处:http://blog.ueren.com/?tag=sysobjects public string GetObjectTypeName(object oType){switch (oType+""){case "U":return "用户表";case "V":retu...
 

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


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 PropertyValue
FROM [{0}].dbo.sysobjects
LEFT JOIN sys.extended_properties E ON E.major_id=[{0}].dbo.sysobjects.id AND E.minor_id=0
Where 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_id
Where B.name=T.name) As ObjectDefinition
FROM [{0}].dbo.sysobjects T
Where xtype in ( N'P', N'PC',N'FN', N'IF', N'TF', N'FS', N'FT', N'V',N'TR')
AND T.name not in ('dtproperties','sysdiagrams')
Order By T.name

查询列与字段:


Select
C.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) From
sysobjects
INNER JOIN sysindexes ON sysobjects.parent_obj=sysindexes.id AND sysobjects.name=sysindexes.name
INNER JOIN sysindexkeys ON sysindexes.id=sysindexkeys.id AND sysindexes.indid=sysindexkeys.indid
Where sysobjects.xtype='PK' AND sysobjects.parent_obj=A.id AND sysindexkeys.colid=A.colid) AS IsPrimaryKey
From
dbo.syscolumns A
INNER JOIN dbo.systypes B ON B.xusertype=A.xtype
INNER 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.id
LEFT JOIN sys.extended_properties E ON E.major_id=A.id AND E.minor_id=A.colid order by A.colid

查询约束脚本:


SELECT
b.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 DeleteCascade
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'

目录
相关文章
|
数据库
数据库数据恢复—MSSQL报错“附加数据库错误823”的数据恢复案例
MSSQL Server数据库比较常见的报错是“附加数据库错误823”。如果数据库有备份,只需要还原备份即可;如果无备份或者备份不可用,则需要使用专业的数据恢复手段去恢复数据。 MSSQL Server数据库出现“823”的报错信息通常情况下有以下三种可能:1、由于数据库的物理页面出现了损坏。2、校验值被损坏导致的数据库页面无法被识别。3、异常断电、文件系统损坏导致的数据库页面丢失。
数据库数据恢复—MSSQL报错“附加数据库错误823”的数据恢复案例
|
3月前
|
Linux 数据库 数据安全/隐私保护
|
2月前
|
SQL 存储 数据库
MSSQL遍历数据库根据列值查询数据
【9月更文挑战第12天】在 SQL Server 中,可以通过游标或临时表遍历数据库并根据列值查询数据。示例展示了如何创建临时表存储数据库名,并通过循环遍历这些名称来执行特定查询。需替换 `YourTableName`、`YourColumnName` 和 `YourValue` 为实际值。此方法要求有足够权限访问各数据库。若无跨库权限,需分别执行查询。
|
4月前
|
SQL 存储 数据库
MSSQL数据库性能调优实战:索引、查询与并发控制的深度剖析
在数据库管理领域,Microsoft SQL Server(MSSQL)的性能调优是保障业务高效运行的核心任务
|
5月前
|
存储 监控 数据管理
数据库原理与应用——简答题练习(数据管理技术发展、数据库主要特征、数据模型、关系模型、实体性之间的关系、DBMS的功能、相关术语解释、数据库系统)
数据库原理与应用——简答题练习(数据管理技术发展、数据库主要特征、数据模型、关系模型、实体性之间的关系、DBMS的功能、相关术语解释、数据库系统)
63 0
|
6月前
|
SQL 存储 关系型数据库
数据库迁移mssql to pgsql之函数转换
数据库迁移mssql to pgsql之函数转换
|
6月前
|
XML SQL 安全
某教程学习笔记(一):08、MSSQL数据库漏洞
某教程学习笔记(一):08、MSSQL数据库漏洞
48 0
|
6月前
|
存储 SQL 安全
MSSQL存储过程:数据库操作的得力助手
MSSQL存储过程:数据库操作的得力助手
66 0
|
6月前
|
存储 定位技术 数据库
开启数据库性能之旅:MSSQL存储过程索引优化深度解析
开启数据库性能之旅:MSSQL存储过程索引优化深度解析
|
SQL 存储 安全
MSSQL之十一 数据库高级编程总结(下)
MSSQL之十一 数据库高级编程总结(下)
93 0
下一篇
无影云桌面