RSS Feed

OBJECT_DEFINITION

OBJECT_DEFINITION in SQL Server

In SQL Server, there are objects like Tables, Views, Stored Procedures etc. Every object has its own definition stored in the database. To view the definition of a particular object one inbuilt function called OBJECT_DEFINITION can be used. This function returns the Transact-SQL source text or the definition of a specified object.

Syntax of OBJECT_DEFINITION:

OBJECT_DEFINITION (object_id);

Where object_id represents the ID of an object which user wanted to use. The type of object_id will be an integer, and it assumes that the object (for which object_id has been passed as parameter) will be in the current database context.

Return Type The return type of this function is NVARCHAR(MAX). That means the whole definition of an object can easily be accommodated in its return type.

OBJECT_DEFINITION can be applicable to the following types of objects:

1. Constraints like Check, Default
2. Stored procedures
3. Scalar functions
4. Rules
5. Triggers
6. Views

The Object_Definition function will return NULL if it gets an error or caller is not having permissions to view definition of the object. A user will have view rights on the metadata of securable which user owns or he has been granted permission.

Examples of Object_Definition:

1) Get the object definition of UDF (User defined function)



2) Get the object definition of a Stored procedure



3) Definition for a System object