Skip to content

Instantly share code, notes, and snippets.

@jbnv
Created June 2, 2015 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbnv/7ad4a7d3cb1092c36d1e to your computer and use it in GitHub Desktop.
Save jbnv/7ad4a7d3cb1092c36d1e to your computer and use it in GitHub Desktop.
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