获取创建SQL Server对象的定义文本
admin
2023-06-06 09:01:21
0次

引用自《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常用语句积累

下一篇:zabbix配置部署

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...