介绍 (Introduction)

In this new article, we will talk about the system views/tables/functions and how to solve common questions using them.

在这篇新文章中,我们将讨论系统视图/表/函数以及如何使用它们解决常见问题。

The system views are views that contain internal information about a Database.

系统视图是包含有关数据库的内部信息的视图。

The master database for example contains information about the SQL Server itself, while the msdb database contain information about the SQL Server agent and each database has its own system views/tables.

例如,主数据库包含有关SQL Server本身的信息,而msdb数据库包含有关SQL Server代理的信息,并且每个数据库都有其自己的系统视图/表。

In this article we will show how to get the list of tables, views, stored procedures, how to get a list of tables of all the databases, how to find a table in multiple datatabases, how to get the list of users, logins, mapped logins, how to detect a fragmentation in a table and more.

在本文中,我们将展示如何获取表,视图,存储过程的列表,如何获取所有数据库的表列表,如何在多个数据库中查找表,如何获取用户列表,登录名,映射的登录信息,如何检测表中的碎片等等。

Let’s start with some common questions about databases that can be solved with the system views/tables/functions:

让我们从关于数据库的一些常见问题开始,这些问题可以通过系统视图/表/函数来解决:

如何获取数据库中的表列表? (How can I get the list of tables in a database?)

The following queries can provide you with that information about the database tables:

以下查询可以为您提供有关数据库表的信息:

Option 1

选项1


SELECT * FROM [INFORMATION_SCHEMA].[TABLES]

Option 2

选项2


SELECT *FROM sysobjects WHERE xtype = 'U'

You can use schema views (the INFORMATION_CHEMA.TABLES) or the sysobjects views directly. According to the books, it is better to use the INFORMATION_SCHEMA views because the internal structure will not change in the future. The sysobjects view contains useful information about the different database objects.

您可以直接使用架构视图(INFORMATION_CHEMA.TABLES)或sysobjects视图。 根据这些书,最好使用INFORMATION_SCHEMA视图,因为内部结构将来不会更改。 sysobjects视图包含有关不同数据库对象的有用信息。

Xtype is the type of object and the possible values are:

Xtype是对象的类型,可能的值为:

SQ = Service Queue
TA = Assembly (CLR) DML trigger
TF = Table-valued-Function
TR = Trigger
TT = Table Type
U = User Table
UQ = Unique Constraint
V = View
X = Extended stored procedure

SQ =服务队列
TA =装配(CLR)DML触发器
TF =表值函数
TR =触发
TT =表格类型
U =用户表
UQ =唯一约束
V =视图
X =扩展存储过程

For more information about the sysobjects view you can go to the references.

有关sysobjects视图的更多信息,请转到参考。

如何获取数据库中的视图列表? (How can I get the list of views in a database?)

The solution is similar than the list of tables. There are 2 options to solve this problem:

解决方案类似于表列表。 有2个选项可以解决此问题:

Option 1

选项1


SELECT *
FROM [INFORMATION_SCHEMA].[VIEWS]

Option 2

选项2


SELECT *FROM sysobjects WHERE xtype = 'V'

[INFORMATION_SCHEMA].[VIEWS] contains information about the views or you can find the information in the sysobjects view.

[INFORMATION_SCHEMA]。[VIEWS]包含有关视图的信息,或者您可以在sysobjects视图中找到该信息。

如何获取数据库中的过程列表? (How can I get the list of procedures in a database?)

There are 2 options to solve this problem:

有2个选项可以解决此问题:


select * from [INFORMATION_SCHEMA].[ROUTINES]
where routine_type='PROCEDURE'

SELECT *FROM sysobjects WHERE xtype = 'P'

[INFORMATION_SCHEMA].[ROUTINES] contains information about stored procedures and functions. The sysobjects is a second option to get this information.

[INFORMATION_SCHEMA]。[ROUTINES]包含有关存储过程和函数的信息。 sysobjects是获取此信息的第二个选项。

如何获得特定表的创建日期? (How can I get the creation date of a specific table?)

The following T-SQL code shows the creation date of the table ProductDocument:

以下T-SQL代码显示表ProductDocument的创建日期:


SELECT crdate
FROM sysobjects
WHERE xtype = 'U'
and
name ='ProductDocument'

Crdate is the creation date and sysobjects is the view that contains most of the database objects including tables, views, stored procedures and functions.

Crdate是创建日期,sysobjects是包含大多数数据库对象(包括表,视图,存储过程和函数)的视图。

如何获得所有数据库中所有表的列表? (How can I get the list of all the tables in all the databases?)

The sp_MSforeachdb, is a very useful stored procedure that helps to work with all the databases. For some reason, this stored procedure is not documented.

sp_MSforeachdb是一个非常有用的存储过程,可帮助处理所有数据库。 由于某种原因,该存储过程未记录在案。


DECLARE @cmd varchar(8000)
SELECT @cmd = 'USE ? SELECT ''?'',name FROM sysobjects WHERE xtype = ''U'''
EXEC sp_MSforeachdb @cmd

The ? is the database name. It will show the result of the query in each database.

? 是数据库名称。 它将在每个数据库中显示查询结果。

如何在所有数据库中搜索表? (How can I search a table in all the databases?)

This is a classical problem. The user created a table, but he does not remember where it was created and there are multiple databases where we need to search.

这是一个经典的问题。 用户创建了一个表,但是他不记得它的创建位置,并且有多个数据库需要搜索。

The following example shows how to find a table named test in all the SQL Server databases. The query will show the database(s) and the table.

下面的示例演示如何在所有SQL Server数据库中查找名为test的表。 查询将显示数据库和表。


DECLARE @cmd varchar(8000)
SELECT @cmd = 'USE ? SELECT ''?'',name FROM sysobjects WHERE xtype = ''U'' and name=''test'''
EXEC sp_MSforeachdb @cmd

如何获得存储过程名称及其代码的列表? (How can I get the list of stored procedures names and their code?)

The syscomments view contains very useful information about the stored procedure, triggers, checks and other SQL Server objects. You can check the code of your procedures, triggers and other objects. The sysobjects contains the stored procedure name and the syscomments the code. This query is very useful to find some words in multiple stored procedures at the same time.

syscomments视图包含有关存储过程,触发器,检查和其他SQL Server对象的非常有用的信息。 您可以检查过程,触发器和其他对象的代码。 sysobjects包含存储过程名称,syscomments包含代码。 此查询对于同时在多个存储过程中查找某些单词非常有用。


SELECT name, [text]FROM sysobjects oINNER JOIN syscomments cON o.id=c.id
WHERE xtype = 'P'

如何获取特定数据库用户的创建日期? (How can I get the creation date of a specific database user? )

The sysusers view contain useful information related to the users in a database. The following sample shows the creation date of the public database user.

sysusers视图包含与数据库中的用户有关的有用信息。 以下示例显示了公共数据库用户的创建日期。


select createdate from [sys].[sysusers]
where name='public'

For more information about the sysusers, go to references.

有关sysuser的更多信息,请转到参考。

如何获取特定SQL Server登录名的创建日期? (How can I get the creation date of a specific SQL Server Login?)

The Login allows to login to the SQL Server database and the database user contains permissions to access to specific databases. The following query shows the creation Date of a specific Login:

登录名允许登录到SQL Server数据库,并且数据库用户包含访问特定数据库的权限。 以下查询显示特定登录名的创建日期:


select * from
[sys].[syslogins]
where name='sa'

如何获得登录名和用户映射到所有数据库中? (How can I get the Login and the user mapped in all the databases?)

This information is in the sys.database.principals and syss,server.principals. You will find the Login and the database user mapped to it.

此信息位于sys.database.principals和syss,server.principals中。 您将找到登录名和映射到该登录名的数据库用户。


DECLARE @cmd varchar(8000)
SELECT @cmd = 'SELECT ''?'',login_name = sp.name, user_name = dp.nameFROM ?.sys.database_principals AS dpINNER JOIN sys.server_principals AS spON dp.[sid] = sp.[sid]'EXEC sp_MSforeachdb @cmd

如何检测特定表的碎片? (How can I detect the fragmentation of a specific table?)

Multiple inserts, deletes can produce fragmentations in the databases. In earlier versions, the DBCC SHOWCONTIG sentences were used to detect fragmentations. Now, we use the sys.dm_db_index_physical_stats function. The column that you need to check is the avg_fragmentation_in_percent. If the percentage is high, you may need to reorganize or rebuild the indexes of your table.

多次插入,删除会在数据库中产生碎片。 在早期版本中,DBCC SHOWCONTIG语句用于检测碎片。 现在,我们使用sys.dm_db_index_physical_stats函数。 您需要检查的列是avg_fragmentation_in_percent。 如果百分比很高,则可能需要重新组织或重建表的索引。

The following sample with shows the fragmentation information about the [Purchasing].[PurchaseOrderDetail table.

以下示例显示了有关[Purchasing]。[PurchaseOrderDetail表]的分段信息。


DECLARE @db_id SMALLINT;
DECLARE @object_id INT;SET @db_id = DB_ID(N'AdventureWorks2012');
SET @object_id = OBJECT_ID('[Purchasing].[PurchaseOrderDetail]');SELECT avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats(@db_id, @object_id, NULL, NULL , 'LIMITED');GO

If the fragmentation percentage is higher than 30%, it is better to rebuild the index. If it is lower than 30%, you can just reorganize the index.

如果碎片百分比大于30%,则最好重建索引。 如果低于30%,则可以重新组织索引。

如何获得有关SQL Server会话的信息? (How can I get the information about the SQL Server sessions?)

The [sys].[dm_exec_sessions] view contains useful information about the sessions like the start time, end time, login name, NT domain, program used and more.

[sys]。[dm_exec_sessions]视图包含有关会话的有用信息,例如开始时间,结束时间,登录名,NT域,使用的程序等。


SELECT *FROM [sys].[dm_exec_sessions]

如何获取有关备份创建日期的信息? (How can I get information about the creation date of a backup?)

You can find the start date and end date of a database backup in the backupset table.

您可以在backupset表中找到数据库备份的开始日期和结束日期。


SELECT[backup_start_date],[backup_finish_date]FROM [msdb].[dbo].[backupset]

如何获得有关MB大小和备份位置的信息? (How can I get information about the size in MB and location of a backup?)

You can also find very useful information about the backup file size and the path of the backup using the backupfile system table. The following sample shows the size of the backups in MB and the path where it is located. This information is stored in the msdn database. The backup file_size is stored in bytes, that is why it is necessary to convert the value to MB.

您还可以使用backupfile系统表找到有关备份文件大小和备份路径的非常有用的信息。 以下示例显示了以MB为单位的备份大小以及备份所在的路径。 此信息存储在msdn数据库中。 备份文件大小以字节为单位存储,这就是为什么有必要将值转换为MB的原因。


SELECT [file_size]/1024/1024 as Size_in_mb,[physical_name]FROM [msdb].[dbo].[backupfile]

如何获取有关SQL Server作业的创建日期和修改日期的信息? (How can I get information about creation date and modification date of a SQL Server Job?)

There is a lot of information about SQL jobs in the MSDB database, dbo.sysjobs table. All the information related to the agent is stored in the MSDB database.

MSDB数据库dbo.sysjobs表中有很多有关SQL作业的信息。 与代理有关的所有信息都存储在MSDB数据库中。


SELECT [date_created],[date_modified]FROM [msdb].[dbo].[sysjobs]

This information can also be displayed using the help_job stored procedure:

也可以使用help_job存储过程显示此信息:

结论 (Conclusion)

The system/table/functions helps us to monitor our database, track changes and measure the performance of the databases. There are multiple uses of these objects to help us in our real life problems.

系统/表/功能可帮助我们监视数据库,跟踪更改并评估数据库的性能。 这些对象有多种用途,可以帮助我们解决现实生活中的问题。

翻译自: https://www.sqlshack.com/sql-server-system-viewstablesfunctions-common-questions-solutions-real-life-problems/

SQL Server系统视图/表/功能。 现实生活中的常见问题和解决方案相关推荐

  1. Microsoft Dynamics server 2015 所有的SQL server 2012 视图 介绍及功能

    微软Dynamics 2015 所有的SQL server 2012 视图 介绍及功能 此文章中SQL Server 视图可以为开发者提供帮助,以下部分摘自微软CRM官方安装文件中. Microsof ...

  2. sql server系统表详细说明(转)

    sysaltfiles  主数据库 保存数据库的文件 syscharsets  主数据库字符集与排序顺序 sysconfigures 主数据库 配置选项 syscurconfigs 主数据库当前配置选 ...

  3. sql server 系统表 介绍

    sysaltfiles  主数据库 保存数据库的文件 syscharsets  主数据库字符集与排序顺序 sysconfigures 主数据库 配置选项 syscurconfigs 主数据库当前配置选 ...

  4. SQL SERVER系统表

    系统表查看 SQL SERVER系统表在系统数据库master中,其实确切的说这些系统表是视图,可以在master库下的视图下查看 常用系统表及功能 sysaltfiles 主数据库 保存数据库的文件 ...

  5. 应用事件探查器优化SQL Server系统[转]

    应用事件探查器优化SQL Server系统 关键词:Mssql,,概述 当你的SQL Server数据库系统运行缓慢的时候,你或许多多少少知道可以使用SQL Server Profiler(中文叫SQ ...

  6. 学习SQL:SQL Server数据透视表

    In the previous few articles in this series, we've set the foundations on how to create a report. We ...

  7. Sql Server 系统的存储过程

    xp_cmdshell –*执行DOS各种命令,结果以文本行返回. xp_fixeddrives –*查询各磁盘/分区可用空间 xp_loginconfig –*报告SQL Server 实例在Win ...

  8. 2.SQL SERVER笔记——SQL SERVER系统概念

    2.SQL SERVER笔记--SQL SERVER系统概念 系统数据库 数据库管理员(DBA)的一项基本的技能是对SQL数据库引擎的系统数据库的深刻理解.数据库开发人员了解SQLSERVER自带的系 ...

  9. 微软服务器sql server,解析:微软SQL Server 2008特性和功能

    导语:SQL Server 2008系统诞生于08年3月13日,在微软2008新一代企业应用平台与开发技术发布大会上,微软宣布向企业用户大众同时发布三款核心应用平台产品:Windows Server ...

最新文章

  1. 【PAT (Advanced Level) Practice】1093 Count PAT‘s (25 分)
  2. 解决三星 BIOS 模式没有 Fast Bios Mode选项 U盘动项问题
  3. 串口同步通信和串口异步通信
  4. TF之AE:AE实现TF自带数据集数字真实值对比AE先encoder后decoder预测数字的精确对比—daidingdaiding
  5. 武大计算机学院推免清北,南大2020年推免生来自哪儿?山大73,武大41,清北5人...
  6. 考研【研究所和高校的区别、全国375所独立于高校系统之外的 “研究生所”】
  7. python类加载器_利用Python反序列化运行加载器实现免杀
  8. 使用spring-data-mongodb操作mongodb
  9. 退火算法 贪婪算法_算法贪婪策略简介
  10. html的table效果,html的table用法(让网页的视觉效果显示出来)
  11. 路由重分布 rip,eigrp,和ospf
  12. 最好用的屏幕录像软件,三步实现高清录制?
  13. SkipList 一种使用概率替代平衡树的数据结构
  14. 用户绑定手机号的思考
  15. 如何使用Lumion创建惊艳的渲染
  16. Word 2016使用技巧
  17. REDO和UNDO详解
  18. Java 监控线程池所有任务是否执行完毕
  19. 联拓生物任命钱江担任中国区总经理
  20. 解锁三星bl锁有几种方法_如何判断三星 Galaxy S7 (G9308)手机bootloader是否解锁_免费解锁BL的3个方法...

热门文章

  1. 4.12_proxy_结构型模式:代理模式
  2. 2.2_queue_队列
  3. 给WPF文字加多条修饰线
  4. [AT2558]Many Moves
  5. ExcelPackage 读取、导出excel
  6. Intellij Idea 15 下新建 Hibernate 项目以及如何添加配置
  7. ubuntu 12 04下安装JDK7
  8. 如何在数据源是空的时候,gridview显示表头(万能)
  9. 简哲,请让我的生活简单一点。——批《输入法破局研究——联盟推广》
  10. JavaScript内存泄漏的几种情况