Ever wondered how a simple task can be done multiple ways. You can script a stored procedure in many ways. You can use SSMS tool and use Generate Script option. However, if you want to do it in T-SQL, there are four ways. Here are the different ways to script procedure code, one by one.

Let us create this simple procedure
CREATE PROCEDURE TESTING (@OPTION INT) AS IF @OPTION=0 SELECT 1 AS NUMBER ELSE SELECT 100 AS NUMBER GO
Now if you want to know the script of the above procedure, you can use the following methods
1 Use SP_HELPTEXT System Stored Procedure
EXEC sp_HELPTEXT TESTING
2 Use ROUTINE_DEFINITION from INFORMATION_SCHEMA.ROUTINES view
SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME='TESTING'
3 Use OBJECT_DEFINITION function
SELECT OBJECT_DEFINITION(OBJECT_ID('TESTING'))
4 Use SYS.SYSCOMMENTS system view
SELECT TEXT FROM SYS.SYSCOMMENTS WHERE ID=OBJECT_ID('TESTING')
All the above four methods will show you the script of the procedure names TESTING.
Which of the Different Ways to Script Procedure Code I Pick
All four methods show the code, but they do not behave the same with long procedures. Here is how I think about them:
- OBJECT_DEFINITION returns the full text as NVARCHAR(MAX), so it is my first choice. It also works for views, functions and triggers.
sys.sql_moduleshas the same full text in itsdefinitioncolumn. - sp_helptext returns the code as many rows. It is easy to read, but very long lines can be split, so check before you copy it back.
- INFORMATION_SCHEMA.ROUTINES keeps only the first 4,000 characters, so a large procedure comes back cut off.
- sys.syscomments is an old compatibility view that stores the text in pieces across several rows. I avoid it in new scripts.
Two more tips. If a procedure was created WITH ENCRYPTION, none of these methods will show its text; you get NULL or a message that the text is encrypted. And if you get NULL for a procedure that is not encrypted, check your permissions, because you need VIEW DEFINITION on the object to see its code.
Finally, when you copy a long definition out of the SSMS grid, make sure nothing got trimmed along the way. Comparing the length with LEN(OBJECT_DEFINITION(OBJECT_ID('dbo.MyProc'))) is a quick way to be sure. If your copy is shorter, use the Generate Scripts option in SSMS for that procedure, which writes out the complete text.
Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.
Discover more from SQL Authority with Pinal Dave
Subscribe to get the latest posts sent to your email.





5 Comments. Leave new
Hi Pinal, I didn’t know about OBJECT_DEFINITION, and we also could use:
SELECT definition FROM sys.sql_modules WHERE object_id = OBJECT_ID(‘TESTING’)
The replacement for sys.syscomments.
Correct Brunno!
Methods 2 & 3 seem to have problems with really large stored procedures.
district_id district_name Population
1 A 1000
2 B 2000
3 C 500
4 A 1500
5 A 2000
6 A 600
procedure with district name as input parameter and rank as other input parameter. Output of that procedure should be as below
if for that procedure district name A and rank 2 are passed as input parameters then output of that procedure should get the 2nd most populated district with name as “A”.
A 1600
How can same district have multiple population?
Anyways, your goal is to find nth largest has multiple solutions.
Read http://blog.sqlauthority.com/2008/04/02/sql-server-find-nth-highest-salary-of-employee-query-to-retrieve-the-nth-maximum-value/