| title | OBJECT_DEFINITION (Transact-SQL) | ||||
|---|---|---|---|---|---|
| description | OBJECT_DEFINITION (Transact-SQL) | ||||
| author | VanMSFT | ||||
| ms.author | vanto | ||||
| ms.date | 03/14/2017 | ||||
| ms.service | sql | ||||
| ms.subservice | t-sql | ||||
| ms.topic | reference | ||||
| ms.custom |
|
||||
| f1_keywords |
|
||||
| helpviewer_keywords |
|
||||
| dev_langs |
|
[!INCLUDE SQL Server Azure SQL Database Azure SQL Managed Instance FabricSQLDB]
Returns the [!INCLUDEtsql] source text of the definition of a specified object.
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions
OBJECT_DEFINITION ( object_id )
object_id
Is the ID of the object to be used. object_id is int, and assumed to represent an object in the current database context.
nvarchar(max)
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. 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. For more information, see Metadata Visibility Configuration.
The [!INCLUDEssDEnoversion] 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 applies to the following object types:
-
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
System object definitions are publicly visible. 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. These permissions are implicitly held by members of the db_owner, db_ddladmin, and db_securityadmin fixed database roles.
The following example returns the definition of a user-defined trigger, uAddress, in the Person schema. The built-in function OBJECT_ID is used to return the object ID of the trigger to the OBJECT_DEFINITION statement.
USE AdventureWorks2022;
GO
SELECT OBJECT_DEFINITION (OBJECT_ID(N'Person.uAddress')) AS [Trigger Definition];
GO The following example returns the definition of the system stored procedure sys.sp_columns.
USE AdventureWorks2022;
GO
SELECT OBJECT_DEFINITION (OBJECT_ID(N'sys.sp_columns')) AS [Object Definition];
GO Metadata Functions (Transact-SQL)
OBJECT_NAME (Transact-SQL)
OBJECT_ID (Transact-SQL)
sp_helptext (Transact-SQL)
sys.sql_modules (Transact-SQL)
sys.server_sql_modules (Transact-SQL)