码迷,mamicode.com
首页 > 数据库 > 详细

获取创建SQL Server对象的定义文本

时间:2016-09-06 23:30:11      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:object_definition   sys.sql_modules   information_schema.routines   

引用自《SQL Server 2012 Internals》

“  
As views, these metadata objects are based on an underlying Transact-SQL (T-SQL) defnition. The most straightforward way to see the defnition of these views is by using the object_defnition function. (You can also see the defnition of these system views by using sp_helptext or by selecting from the catalog view sys.system_sql_modules.) So to see the defnition of sys.tables, you can execute the following:

SELECT object_definition (object_id(‘sys.tables‘));  

 

方法一:

OBJECT_DEFINITION

返回指定对象的定义的 Transact-SQL 源文本。

SELECT object_definition (object_id(‘sys.tables‘));

https://msdn.microsoft.com/zh-cn/library/ms176090.aspx

 

方法二:

sp_helptext

显示用户定义规则的定义、默认值、未加密的 Transact-SQL 存储过程、用户定义 Transact-SQL 函数、触发器、计算列、CHECK 约束、视图或系统对象(如系统存储过程)。

EXEC sp_helptext ‘sys.tables‘;  
GO

https://msdn.microsoft.com/zh-cn/library/ms176112.aspx

 

方法三:

sys.system_sql_modules

为每个包含 SQL 语言定义模块的系统对象返回一行。 类型为 FN、IF、P、PC、TF 和 V 的系统对象具有关联的 SQL 模块。 若要标识该包含对象,可以将该视图联接到 sys.system_objects。

SELECT ssm.object_id,    
    OBJECT_NAME(ssm.object_id) AS object_name,     
    SCHEMA_NAME(t.schema_id) AS schema_name,     
    t.type,     
    t.type_desc,     
    ssm.definition    
FROM sys.system_sql_modules ssm    
    INNER JOIN sys.system_objects t ON ssm.object_id = t.object_id    
WHERE t.type=‘P‘    
GO

https://msdn.microsoft.com/zh-cn/library/ms188034.aspx


示例:  

select object_name(m.object_id) as name, * 
from sys.system_sql_modules m    
inner join sys.system_objects t on m.object_id=t.object_id    
where type=‘P‘ and name=‘sp_renamedb‘

select object_name(m.object_id) as name, * 
from sys.system_sql_modules m   
inner join sys.system_objects t on m.object_id=t.object_id    
where type=‘V‘ and name=‘systypes‘


sys.sql_modules

对每个 SQL 语言定义的模块对象都返回一行。类型为 P、RF、V、TR、FN、IF、TF 和 R 的对象均有关联的 SQL 模块。在此视图中,独立的默认值,即 D 类型的对象也具有 SQL 模块定义。有关这些类型的说明,请参阅 sys.objects 目录视图中的类型列。

SELECT sm.object_id,    
    OBJECT_NAME(sm.object_id) AS object_name,     
    SCHEMA_NAME(o.schema_id) AS schema_name,     
    o.type,     
    o.type_desc,     
    sm.definition    
FROM sys.sql_modules AS sm    
    INNER JOIN sys.objects AS o ON sm.object_id = o.object_id    
ORDER BY o.type;    
GO

https://technet.microsoft.com/zh-cn/library/ms175081.aspx


示例:  

SELECT OBJECT_NAME(object_id)    
    FROM sys.sql_modules    
    WHERE OBJECTPROPERTY(object_id, ‘IsProcedure‘) = 1    
    AND definition LIKE ‘%yourText%‘


sys.all_sql_modules

返回 sys.sql_modules 和 sys.system_sql_modules 的联合。


https://msdn.microsoft.com/zh-cn/library/ms184389.aspx

 

方法四:

INFORMATION_SCHEMA

信息架构视图是 SQL Server 提供的几种获取元数据的方法之一。 信息架构视图提供独立于系统表的内部 SQL Server 元数据视图。 尽管已经对基础系统表进行了重要的修改,信息架构视图仍然可使应用程序正常工作。 SQL Server 中包含的信息架构视图符合 ISO 标准中的信息架构定义。


在引用当前服务器时,SQL Server 支持三部分命名约定。 ISO 标准也支持三部分命名约定。 但是,两种命名约定中使用的名称并不相同。 信息架构视图是在名为 INFORMATION_SCHEMA 的特殊架构中定义的。 此架构包含在每个数据库中。 每个信息架构视图包含特定数据库中存储的所有数据对象的元数据。


下表显示了 SQL Server 名称和 SQL 标准名称之间的关系。

SQL Server 名称

对应的 SQL 标准等价名称

数据库

目录

架构

架构

Object

Object

用户定义数据类型


引用信息架构视图时,必须使用包含 INFORMATION_SCHEMA 架构名称的限定名。

SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT
FROM AdventureWorks2014.INFORMATION_SCHEMA.COLUMNS    
WHERE TABLE_NAME = N‘Product‘;    
GO

https://msdn.microsoft.com/zh-cn/library/ms186778.aspx


INFORMATION_SCHEMA.ROUTINES

为当前数据库中可由当前用户访问的每个存储过程及函数返回一行。 描述返回值的列只适用于函数。 对于存储过程,这些列将为 NULL。


若要从这些视图中检索信息,请指定 INFORMATION_SCHEMA.view_name 的完全限定名称。


ROUTINE_DEFINITION

nvarchar(4000) 
列包含创建函数或存储过程的源语句。 这些源语句有可能包含嵌入式回车符。 如果将此列返回给某个以文本格式显示结果的应用程序,则 ROUTINE_DEFINITION 结果中的嵌入式回车符可能会影响整个结果集的格式。 如果选择 ROUTINE_DEFINITION 列,则必须对嵌入式回车符进行调整,例如,可将结果集返回到一个网格中或者将 ROUTINE_DEFINITION 返回到其自己的文本框中。

如果函数或存储过程未加密,返回函数或存储过程的定义文本最前面的 4000 字符。 否则,返回 NULL。

若要确保获得完整定义,请查询 OBJECT_DEFINITION 函数或 sys.sql_modules 目录视图中的 definition 列。


SELECT   
SPECIFIC_CATALOG,SPECIFIC_SCHEMA,SPECIFIC_NAME,ROUTINE_TYPE    
FROM INFORMATION_SCHEMA.ROUTINES    
WHERE ROUTINE_DEFINITION LIKE ‘%yourText%‘;

https://msdn.microsoft.com/zh-cn/library/ms188757.aspx


引用自《SQL Server 2012 Internals》


Information schema views
Information schema views, introduced in SQL Server 7.0, were the original system table-independent view of the SQL Server metadata. The information schema views included in SQL Server 2012 comply with the SQL-92 standard, and all these views are in a schema called INFORMATION_SCHEMA. Some information available through the catalog views is available through the information schema views, and if you need to write a portable application that accesses the metadata, you should consider using these objects. However, the information schema views show only objects compatible with the SQL-92 standard. This means no information schema view exists for certain features, such as indexes, which aren’t defned in the standard. (Indexes are an implementation detail.) If your code doesn’t need to
be strictly portable, or if you need metadata about nonstandard features such as indexes, flegroups, the CLR, and SQL Server Service Broker, using the Microsoft-supplied catalog views is suggested. Most examples in the documentation, as well as in this and other reference books, are based on the catalog view interface.


MySQL中的 INFORMATION_SCHEMA Tables

INFORMATION_SCHEMA provides access to database metadata, information about the MySQL server such as the name of a database or table, the data type of a column, or access privileges. Other terms that are sometimes used for this information are data dictionary and system catalog.


http://dev.mysql.com/doc/refman/5.7/en/information-schema.html


本文出自 “SQL Server Deep Dive” 博客,请务必保留此出处http://ultrasql.blog.51cto.com/9591438/1846863

获取创建SQL Server对象的定义文本

标签:object_definition   sys.sql_modules   information_schema.routines   

原文地址:http://ultrasql.blog.51cto.com/9591438/1846863

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!