Rajanand Ilangovan
Rajanand Ilangovan

Rajanand Ilangovan

How to get any object's definition in SQL Server?

How to get any object's definition in SQL Server?

Stored procedures, Views, Functions and Trigger

Rajanand Ilangovan's photo
Rajanand Ilangovan
·Mar 4, 2022·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

Query

USE AdventureWorks2019;
GO
SELECT object_name(object_id) as stored_procedure_name,
definition
FROM sys.sql_modules
WHERE object_name(object_id) IN (select name from sys.procedures);

SELECT object_name(object_id) as view_name,
definition
FROM sys.sql_modules
WHERE object_name(object_id) IN (select name from sys.views);

SELECT object_name(object_id) as trigger_name,
definition
FROM sys.sql_modules
WHERE object_name(object_id) IN (select name from sys.triggers);

SELECT object_name(object_id) as function_name,
definition
FROM sys.sql_modules
WHERE object_name(object_id) IN 
    (select name from sys.objects where type IN ('FN','IF','TF'));

Sample Output

image.png