views:

36

answers:

1

Hello,

I have a database which contains 100's of stored procedures. I want to find which the longest stored procedure in these. How can i find it?

Currently i am using

Select text,MAX(len(text)) from syscomments group by text;

But i am not able to find the name of stored procedure in it.

Thanks in advance :)

+5  A: 

sys.comments divides long definitions into 4000 character chunks. Use sys.sql_modules to avoid this issue.

SELECT TOP 1 
    OBJECT_NAME(object_id) AS Name, 
    LEN(definition) AS Length, 
    CAST((SELECT definition AS [processing-instruction(x)] FOR XML PATH('')) AS XML) AS Definition
FROM sys.sql_modules
WHERE OBJECTPROPERTY(object_id, 'IsProcedure')=1
ORDER BY LEN(definition) DESC
Martin Smith
+1, might want to add a `, LEN(definition)` to the select list just for the fun of it!
KM
Great! I got 43103 :D
Ankit Rathod
For Bonus fun I've added a column that will retrieve the whole definition. (The XML trick avoids it being truncated in management studio)
Martin Smith