1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > SQL Server系统视图/表/功能。 现实生活中的常见问题和解决方案

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

时间:2023-09-17 22:39:21

相关推荐

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

介绍 (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.idWHERE 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'AdventureWorks');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.

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

翻译自: /sql-server-system-viewstablesfunctions-common-questions-solutions-real-life-problems/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。