数据库对象,特别是使用SQL 语言编写的功能模块,例如,P(SQL Stored Procedure), V(View), TR(SQL DML trigger), FN(SQL scalar function), IF(SQL inline table-valued function), TF(SQL table-valued-function)等,都是通过SQL编程实现,SQL Server存储其定义的实现。
1, 查看用户创建的功能模块的定义
系统视图:sys.sql_modules 返回每个使用SQL定义的功能模块,字段definition返回模块的定义脚本:
select sm.object_id, o.name as object_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
如果要查看特定类型的功能模块的定义,可以通过object的type来过滤。
2,查看单个数据库对象的定义
使用 sp_helptext 和 object_definition ,能够查看单个数据库对象的定义:
select object_definition(object_id('sys.tables')) exec sp_helptext 'sys.tables'
其中,函数 OBJECT_DEFINITION 能够查看的数据库对象的类型是:
- C = Check constraint
- D = Default (constraint or stand-alone)
- P = SQL stored procedure
- FN = SQL scalar function
- R = Rule
- RF = Replication filter procedure
- TR = SQL trigger (schema-scoped DML trigger, or DDL trigger at either the database or server scope)
- IF = SQL inline table-valued function
- TF = SQL table-valued function
- V = View
参考文档: