Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SQL Server query to return the code for an object by name.
SELECT [Item]
FROM sys.sql_modules
CROSS APPLY (
SELECT ROW_NUMBER() OVER(ORDER BY N) AS [Ordinal],
SUBSTRING([definition], N, CHARINDEX(CHAR(13), [definition] + CHAR(13), N) - N) AS [Item]
FROM dbo.Numbers
WHERE N <= CONVERT(INT, LEN([definition]))
AND SUBSTRING(CHAR(13) + [definition], N, 1) = CHAR(13)
) t
WHERE OBJECT_NAME(object_id) = 'OBJECTNAME'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.