适用于: 是SQL Server是Azure SQL 数据库否Azure SQL 数据仓库否并行数据仓库APPLIES TO: yesSQL Server yesAzure SQL Database noAzure SQL Data Warehouse noParallel Data Warehouse

返回指定对象的定义的 Transact-SQLTransact-SQL 源文本。Returns the Transact-SQLTransact-SQL source text of the definition of a specified object.

主题链接图标 TRANSACT-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions


OBJECT_DEFINITION ( object_id )  


要使用的对象的 ID。Is the ID of the object to be used. object_id 的数据类型为 int,并假定表示当前数据库上下文中的对象。object_id is int, and assumed to represent an object in the current database context.

返回类型Return Types



出现错误时或调用方没有查看对象的权限时,将返回 NULL。Returns NULL on error or if a caller does not have permission to view the object.

用户只能查看符合如下条件的安全对象的元数据:该安全对象为该用户所有,或已授予该用户对该安全对象的权限。A user can only view the metadata of securables that the user owns or on which the user has been granted permission. 这意味着,如果用户对对象没有任何权限,则元数据生成的内置函数(如 OBJECT_DEFINITION)可能返回 NULL。This means that metadata-emitting, built-in functions such as OBJECT_DEFINITION may return NULL if the user does not have any permission on the object. 有关详细信息,请参阅 Metadata Visibility ConfigurationFor more information, see Metadata Visibility Configuration.


SQL Server 数据库引擎SQL Server Database Engine 假定 object_id 位于当前数据库上下文中。The SQL Server 数据库引擎SQL Server Database Engine assumes that object_id is in the current database context. 对象定义的排序规则始终与调用数据库上下文的排序规则匹配。The collation of the object definition always matches that of the calling database context.

OBJECT_DEFINITION 适用于以下对象类型:OBJECT_DEFINITION applies to the following object types:

  • C = 检查约束C = Check constraint

  • D = 默认值(约束或独立)D = Default (constraint or stand-alone)

  • P = SQL 存储过程P = SQL stored procedure

  • FN = SQL 标量函数FN = SQL scalar function

  • R = 规则R = Rule

  • RF = 复制筛选器过程RF = Replication filter procedure

  • TR = SQL 触发器(架构范围内的 DML 触发器,或数据库或服务器范围内的 DDL 触发器)TR = SQL trigger (schema-scoped DML trigger, or DDL trigger at either the database or server scope)

  • IF = SQL 内联表值函数IF = SQL inline table-valued function

  • TF = SQL 表值函数TF = SQL table-valued function

  • V = 视图V = View


系统对象定义对所有用户可见。System object definitions are publicly visible. 用户对象的定义对于对象所有者或具有下列任一权限的被授权者可见:ALTER、CONTROL、TAKE OWNERSHIP 或 VIEW DEFINITION。The definition of user objects is visible to the object owner or grantees that have any one of the following permissions: ALTER, CONTROL, TAKE OWNERSHIP, or VIEW DEFINITION. db_ownerdb_ddladmindb_securityadmin 固定数据库角色的成员隐式具有这些权限。These permissions are implicitly held by members of the db_owner, db_ddladmin, and db_securityadmin fixed database roles.


A.A. 返回用户定义对象的源文本Returning the source text of a user-defined object

以下示例返回 uAddress 架构中的用户定义触发器 Person 的定义。The following example returns the definition of a user-defined trigger, uAddress, in the Person schema. 内置函数 OBJECT_ID 用于将触发器的对象 ID 返回到 OBJECT_DEFINITION 语句。The built-in function OBJECT_ID is used to return the object ID of the trigger to the OBJECT_DEFINITION statement.

USE AdventureWorks2012;  
SELECT OBJECT_DEFINITION (OBJECT_ID(N'Person.uAddress')) AS [Trigger Definition];   

B.B. 返回系统对象的源文本Returning the source text of a system object

以下示例返回系统存储过程 sys.sp_columns 的定义。The following example returns the definition of the system stored procedure sys.sp_columns.

USE AdventureWorks2012;  
SELECT OBJECT_DEFINITION (OBJECT_ID(N'sys.sp_columns')) AS [Object Definition];  

另请参阅See Also

元数据函数 (Transact-SQL) Metadata Functions (Transact-SQL)
OBJECT_ID (Transact-SQL) OBJECT_ID (Transact-SQL)
sp_helptext (Transact-SQL) sp_helptext (Transact-SQL)
sys.sql_modules (Transact-SQL) sys.sql_modules (Transact-SQL)
sys.server_sql_modules (Transact-SQL)sys.server_sql_modules (Transact-SQL)